<分区>
我需要一种方法来将数组拆分为精确数量且大小大致相等的较小数组。谁有办法做到这一点?
例如
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
groups = a.method_i_need(3)
groups.inspect
=> [[1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
Note that this is an entirely separate problem from dividing an array into chunks, because
a.each_slice(3).to_a
would produce 5 groups (not 3, like we desire) and the final group may be a completely different size than the others:[[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13]] # this is NOT desired here.
在这个问题中,预先指定了所需的 block 数,每个 block 的大小最多相差1。