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