So say I have an array l
:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
which holds some set of values, in this case integers. And I have another array m
:
[2, 3, 2]
which holds divisors (I'll explain what I mean by divisor later). What I would like to do is successively divide l
into slices based on the current value in m
. So in this example, I'd first divide it into 2 lists (m[0]
is 2) of equal size:
[[1,2,3,4,5,6,7,8,9,10,11,12], [13,14,15,16,17,18,19,20,21,22,23,24]]
Then divide those lists into 3 (m[1]
is 3) lists, yielding:
[[[1,2,3,4],[5,6,7,8],[9,10,11,12]], [[13,14,15,16],[17,18,19,20],[21,22,23,24]]]
Then finally divide each of those lists into 2 lists (m[2]
is 2):
[[[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]],[[[13,14],[15,16]],[[17,18],[19,20]],[[21,22],[23,24]]]]
In the actual implementation of this, m
will be of an arbitrary length and have arbitrary values.
Is there an existing algorithm that accomplishes this? This will basically be a tree structure, as each list that results from a division will receive a label, but I'm not sure how to implement a function that will return the list I need. I'm implementing in Javascript, but the algorithm is what's important here.