Michal Gabrukiewicz
2008-Jan-07 13:18 UTC
nice way to split an array into 3 equally sized pieces
before i start writing my own sophisticated small algorithm i thought i ask you guys how to do this in a rails way .. given array: a = [1, 2, 3, 4, 5, 6, 7] out of this array i need an array b holding three elements where each of them holds the same size of elements ... if not possible then the last one should hold less .. so for the above it would be b = [[1, 2, 3], [4, 5, 6], [7]] another example: a = [1, 2] => b = [[1], [2], []] thanks for the simplest way -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-07 13:57 UTC
Re: nice way to split an array into 3 equally sized pieces
On 7 Jan 2008, at 13:18, Michal Gabrukiewicz wrote:> > before i start writing my own sophisticated small algorithm i > thought i > ask you guys how to do this in a rails way .. > > given array: > a = [1, 2, 3, 4, 5, 6, 7] > > out of this array i need an array b holding three elements where > each of > them holds the same size of elements ... if not possible then the last > one should hold less .. so for the above it would be > > b = [[1, 2, 3], [4, 5, 6], [7]] > > another example: > a = [1, 2] => b = [[1], [2], []] >Well not massively nice but def split(array) length = (array.length / 3.0).ceil [array[0, length], array[length, 2*length], array[2*length, 3*length]] end Fred
Phillip Koebbe
2008-Jan-07 14:17 UTC
Re: nice way to split an array into 3 equally sized pieces
On Jan 7, 2008, at 7:18 AM, Michal Gabrukiewicz wrote:> > before i start writing my own sophisticated small algorithm i > thought i > ask you guys how to do this in a rails way .. > > given array: > a = [1, 2, 3, 4, 5, 6, 7] > > out of this array i need an array b holding three elements where > each of > them holds the same size of elements ... if not possible then the last > one should hold less .. so for the above it would be > > b = [[1, 2, 3], [4, 5, 6], [7]] > > another example: > a = [1, 2] => b = [[1], [2], []] > > thanks for the simplest way >in_groups_of a = [1,2,3,4,5,6,7] a.in_groups_of(3) [[1,2,3], [4,5,6], [7,nil,nil]] or a.in_groups_of(3, false) [[1,2,3], [4,5,6], [7]] Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michal Gabrukiewicz
2008-Jan-07 15:42 UTC
Re: nice way to split an array into 3 equally sized pieces
Phillip Koebbe wrote:> On Jan 7, 2008, at 7:18 AM, Michal Gabrukiewicz wrote: > >> them holds the same size of elements ... if not possible then the last >> one should hold less .. so for the above it would be >> >> b = [[1, 2, 3], [4, 5, 6], [7]] >> >> another example: >> a = [1, 2] => b = [[1], [2], []] >> >> thanks for the simplest way >> > > in_groups_of >thanks philip but this does it exactly the other way round .. fred''s version is fine but it was a bit buggy .. this works fine #splits an array into 3 equal parts def split_array(array) length = (array.length / 3.0).ceil [array[0, length], array[length, length], array[2 * length, length]] end -- 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2008-Jan-07 17:32 UTC
Re: nice way to split an array into 3 equally sized pieces
Michal Gabrukiewicz wrote:> thanks philip but this does it exactly the other way round .. fred''s > version is fine but it was a bit buggy .. this works fine > > #splits an array into 3 equal parts > def split_array(array) > length = (array.length / 3.0).ceil > [array[0, length], array[length, length], array[2 * length, length]] > endYou know, if I had been paying attention, I would have done two things differently: 1) ran your second example 2) noticed that Fred did *not* suggest in_groups_of Either one of those would have been sufficient to tell me that my idea was not correct. <crawling back into his cave> Peace, Phillip -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-07 17:46 UTC
Re: nice way to split an array into 3 equally sized pieces
On 7 Jan 2008, at 15:42, Michal Gabrukiewicz wrote:> > Phillip Koebbe wrote: >> On Jan 7, 2008, at 7:18 AM, Michal Gabrukiewicz wrote: >> >>> them holds the same size of elements ... if not possible then the >>> last >>> one should hold less .. so for the above it would be >>> >>> b = [[1, 2, 3], [4, 5, 6], [7]] >>> >>> another example: >>> a = [1, 2] => b = [[1], [2], []] >>> >>> thanks for the simplest way >>> >> >> in_groups_of >> > > thanks philip but this does it exactly the other way round .. fred''s > version is fine but it was a bit buggy .. this works fine > > #splits an array into 3 equal parts > def split_array(array) > length = (array.length / 3.0).ceil > [array[0, length], array[length, length], array[2 * length, > length]] > endOops, I obviously went on crack when I typed it into my mail client :-) Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Morgan Christiansson
2009-Oct-03 20:18 UTC
Re: nice way to split an array into 3 equally sized pieces
>> [1, 2, 3, 4, 5, 6, 7].in_groups(3)=> [[1, 2, 3], [4, 5, nil], [6, 7, nil]] Note that the in_groups takes 1 from each of the last 2 columns instead of 2 from the last. -- Posted via http://www.ruby-forum.com/.
Colin Law
2009-Oct-03 20:24 UTC
Re: nice way to split an array into 3 equally sized pieces
2009/10/3 Morgan Christiansson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> >>> [1, 2, 3, 4, 5, 6, 7].in_groups(3) > => [[1, 2, 3], [4, 5, nil], [6, 7, nil]] > > Note that the in_groups takes 1 from each of the last 2 columns instead > of 2 from the last.That is as documented, you want in_groups_of Colin