Prev: [ANN] kramdown 0.6.0 released
Next: Custom Exceptions
From: Shreyas Satish on 6 Apr 2010 08:50 I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]] i've to compare all the 4 sub-arrays and get common elements.Next,take 3 subarrays at a time and get common elements.then take 2 sub arrays at a time and get common elements. -- Posted via http://www.ruby-forum.com/.
From: Jean-Julien Fleck on 6 Apr 2010 09:02 Hello, 2010/4/6 Shreyas Satish <shreyas.satish(a)gmail.com>: > I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]] > i've to compare all the 4 sub-arrays and get common elements.Next,take 3 > subarrays at a time and get common elements.then take 2 sub arrays at a > time and get common elements. Have a look to Array#combination to group your subarrays into comparison sets and then use the Set class (require 'set') and it's intersection instance method to get the common elements. Cheers, -- JJ Fleck PCSI1 Lycée Kléber
From: Aldric Giacomoni on 6 Apr 2010 09:05 Shreyas Satish wrote: > I have a multi-dimensional array ; a=[[2,3,4],[1,3,4],[1,2],[1,2,3,4]] > i've to compare all the 4 sub-arrays and get common elements.Next,take 3 > subarrays at a time and get common elements.then take 2 sub arrays at a > time and get common elements. Well, the way to get common elements between arrays is with the "intersection" method: http://ruby-doc.org/core/classes/Array.html#M002212 (of course, available as a Set operation too: http://ruby-doc.org/core/classes/Set.html#M001621) And what you're looking for is a brute-force way to compare all subsets of all possible sizes with other subsets of the same size. I'll leave that one as an exercise. -- Posted via http://www.ruby-forum.com/.
|
Pages: 1 Prev: [ANN] kramdown 0.6.0 released Next: Custom Exceptions |