Results 1 to 2 of 2
Jun 23, 2014, 07:30 #1
- Join Date
- Dec 2004
- 0 Post(s)
- 0 Thread(s)
Arranging collection into groups of 12 (values of 2 or 4)
So I have a collection of objects, each has a value of either 2 or 4. I'm wanting to group them into collections that equal 12.
For instance, if the collection begins:
[4,4,2] and the next object is 4, I would like to skip and insert a '2' object, leaving the 4 for the next group.
I've done some googling and saw someone use shuffle and shift http://stackoverflow.com/questions/2...emainders-ruby, but this is just grouping to sets of 4 with remainders being evenly distributed.
Any ideas on where to start with this would be most appreciated.
Thanksyour brain reacts in the same way whether you are
looking at something or thinking about it...
Jun 24, 2014, 07:35 #2
- Join Date
- Jul 2006
- Victoria, Australia
- 28 Post(s)
- 2 Thread(s)
It sounds like you're wanting a recursive function which would loop through and append groups of 12 to an array, calling itself until all of the numbers are accounted for.
puts get_twelves([2,4,4,2,2,2,4,2. etc...]) def get_twelves(nums, groups=)
Look through recursive function examples like reduce to understand how they work - they're known to be brain benders.