i''m trying to figure out an elegant way of taking an array or enumerable object and dividing it as even as possible into sub arrays. in_groups_of works fine if i''m trying to define the number of objects inside each sub array, but what if i just want to define how many sub arrays instead? is there a method for something like that or would i need to write something custom? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Josh
2008-Apr-02 17:45 UTC
Re: looking for something similar to in_groups_of but not really
i found the transpose method which works unless i pass false to the options of in_groups_of.>> users.in_groups_of(3, false).transposereturns: IndexError: element size differs (2 should be 3) from (irb):19:in `transpose'' from (irb):19 from :0 are there any workarounds for this? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Apr-02 17:48 UTC
Re: looking for something similar to in_groups_of but not re
X in_groups_of Y = Z, and you want Z to be fixed... Assuming you know X (how big the array/collection is) and Z (the desired number of sub-arrays), the math is pretty simple. Something like: in_groups_of ((X/Z)+0.9999).to_i or something like that -- 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 -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Apr-02 18:01 UTC
Re: looking for something similar to in_groups_of but not re
Hmm... maybe not quite that easy... that formula leaves some holes in the distribution of elements... -- 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 -~----------~----~----~----~------~----~------~--~---
Trevor Squires
2008-Apr-03 03:47 UTC
Re: looking for something similar to in_groups_of but not really
Hey, assuming an arbitrary sized array (I chose 100 elements below) and you want to split it all up into at most 3 groups: x = (1..100).to_a x.in_groups_of((x.length.to_f / 3).ceil, false) HTH, Trevor On 4/2/08, Josh <jjkiesch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > i found the transpose method which works unless i pass false to the > options of in_groups_of. > > >> users.in_groups_of(3, false).transpose > > returns: > > IndexError: element size differs (2 should be 3) > from (irb):19:in `transpose'' > from (irb):19 > from :0 > > are there any workarounds for this? > > > > >-- -- Trevor Squires http://somethinglearned.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 -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Apr-03 13:39 UTC
Re: looking for something similar to in_groups_of but not re
Josh Kieschnick wrote:> i''m trying to figure out an elegant way of taking an array or > enumerable object and dividing it as even as possible into sub arrays. >Is your notion of "as even as possible" something like you want three sub-arrays to fill like this (where items are distributes as evenly as possible)? Item Count SubArrays 1 sa1 = [1] sa2 = [] sa3 = [] 2 sa1 = [1] sa2 = [2] sa3 = [] 3 sa1 = [1] sa2 = [2] sa3 = [3] 4 sa1 = [1,2] sa2 = [3] sa3 = [4] 5 sa1 = [1,2] sa2 = [3,4] sa3 = [5] 6 sa1 = [1,2] sa2 = [3,4] sa3 = [5,6] 7 sa1 = [1,2,3] sa2 = [4,5] sa3 = [6,7] 8 sa1 = [1,2,3] sa2 = [4,5,6] sa3 = [7,8] 9 sa1 = [1,2,3] sa2 = [4,5,6] sa3 = [7,8,9] 10 sa1 = [1,2,3,4] sa2 = [5,6,7] sa3 = [8,9,10] or as a dynamic number for in_groups_of would do? where g = (len/3).ceil array.len g SubArrays 1 1 sa1 = [1] sa2 = [] sa3 = [] 2 1 sa1 = [1] sa2 = [2] sa3 = [] 3 1 sa1 = [1] sa2 = [2] sa3 = [3] 4 2 sa1 = [1,2] sa2 = [3,4] sa3 = [] 5 2 sa1 = [1,2] sa2 = [3,4] sa3 = [5] 6 2 sa1 = [1,2] sa2 = [3,4] sa3 = [5,6] 7 3 sa1 = [1,2,3] sa2 = [4,5,6] sa3 = [7] 8 3 sa1 = [1,2,3] sa2 = [4,5,6] sa3 = [7,8] 9 3 sa1 = [1,2,3] sa2 = [4,5,6] sa3 = [7,8,9] 10 4 sa1 = [1,2,3,4] sa2 = [5,6,7,8] sa3 = [9,10] in_groups_of fills early rows rather than evenly distributing... note the ''holes'' at 4, 7, 10, ... that''s what prompted my last post yesterday -- 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 -~----------~----~----~----~------~----~------~--~---
Ar Chron
2008-Apr-03 13:47 UTC
Re: looking for something similar to in_groups_of but not re
You might want to check this out: http://refactormycode.com/codes/167-split_in_groups-instead-of-in_groups_of -- 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 -~----------~----~----~----~------~----~------~--~---
exactly what i was looking for! thanks. On Apr 3, 8:47 am, Ar Chron <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> You might want to check this out: > > http://refactormycode.com/codes/167-split_in_groups-instead-of-in_gro... > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---