Hi, I want to find the combination of array elements like for example a = [1,2,3,4] the combinations are [1,2,3,4] [1,3,4,2] [1,2,3] etc.. If anybody knows plz reply me -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Do you want to know the number of possible combinations or the combinations themselves? On Jul 6, 11:57 pm, "Pragash Mr." <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > I want to find the combination of array elements > > like for example > a = [1,2,3,4] > the combinations are > [1,2,3,4] > [1,3,4,2] > [1,2,3] > etc.. > > If anybody knows plz reply me > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pragash Mr. wrote:> Hi, > I want to find the combination of array elements > > like for example > a = [1,2,3,4] > the combinations are > [1,2,3,4] > [1,3,4,2] > [1,2,3] > etc.. > > > If anybody knows plz reply meThink this works, def combinations(array) return [] if array == [] return [array, []] if array.length == 1 # split [1,2,3] => [1], [2,3] left, right = array[0..0], array[1..-1] rtn = [] combinations(right).each do |r_array| rtn << r_array rtn << left + r_array end return rtn end>> combinations([1])=> [[1]]>> combinations([1])=> [[1], []]>> combinations([1,2])=> [[2], [1, 2], [], [1]]>> combinations([1,2,3])=> [[3], [1, 3], [2, 3], [1, 2, 3], [], [1], [2], [1, 2]] enjoy. http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---