Looking for something similar to in_groups_of but not really

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?

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?

Hmm… maybe not quite that easy… that formula leaves some holes in
the distribution of elements…

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

Josh K. 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

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 [email protected] wrote:

    from (irb):19
    from :0

are there any workarounds for this?

Trevor S.
http://somethinglearned.com

You might want to check this out:

http://refactormycode.com/codes/167-split_in_groups-instead-of-in_groups_of

exactly what i was looking for! thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs