2009/11/4 Jes Gabriel y Gal <jgabrielygalan / gmail.com>:
> On Wed, Nov 4, 2009 at 11:49 AM, George George
> <george.githinji / gmail.com> wrote:
>> Given an array of arrays for example
>> [["A", "B"], ["A", "B", "D", "C"], ["B", "D"], ["B", "C", "F", "E"],
>> ["C", "F"], ["C", "E"], ["G"]]
>>
>> what would be a nice way of "merging" the items that seem to already be
>> contained in another array. e.g.
>> ["A","B"] and ["B", "D"] are subsets of ["A", "B", "D", "C"] I would
>> like to drop the subsets nd be left with ["A", "B", "D", "C"] only.
>
> This is one way:
>
> irb(main):025:0> a = [["A", "B"], ["A", "B", "D", "C"], ["B", "D"],
> ["B", "C", "F", "E"],["C", "F"], ["C", "E"], ["G"]]
> => [["A", "B"], ["A", "B", "D", "C"], ["B", "D"], ["B", "C", "F",
> "E"], ["C", "F"], ["C", "E"], ["G"]]
> irb(main):026:0> a.delete_if {|x| a.any? {|y| (x != y) && ((x&y).size
> == x.size)}}
> => [["A", "B", "D", "C"], ["B", "C", "F", "E"], ["G"]]
>
> Maybe there would be a way to optimize this sorting by size and
> checking smaller ones against bigger ones only.
> Left as an excercise for the reader :-)

Done. :-)

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/