Let's say I have a non-empty list (array):
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
I want to incrementally slice the list into incremented amount of elements per chunk.
For this array, my desired output is:
[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]
What would be the best way to slice the array in multiple chunks.
This is code-golf, so shortest code in bytes wins!
Note: For the final chunk, if the number of elements left in r
is less than what the incremented output requires, the last chunk should just be all that remains in the list.
Test cases:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-> [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]
l = [10, 20, 30, 40]
-> [[10], [20, 30], [40]]
l = [100, 200, 300, 400, 500]
-> [[100], [200, 300], [400, 500]]