2009/11/4 Jes=FAs Gabriel y Gal=E1n <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 =A0and be left with ["A", "B", "D", "C"] only.
>
> This is one way:
>
> irb(main):025:0> a =3D [["A", "B"], ["A", "B", "D", "C"], ["B", "D"],
> ["B", "C", "F", "E"],["C", "F"], ["C", "E"], ["G"]]
> =3D> [["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 !=3D y) && ((x&y).size
> =3D=3D x.size)}}
> =3D> [["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


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