Given an input list. Example [1,2,3,4,5,6]
split it into a new list, where the items contained are split into longer and longer length lists in the parent list. Like: [[1], [2, 3], [4, 5, 6]]
.
If the list does not fill the chunk completly, fill it up party. [1,2,3,4]
=> [[1], [2, 3], [4]]
More examples:
[1, 2, 3, 4, 5, 6]
=> [[1], [2, 3], [4, 5, 6]]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
=> [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15], [16]]
def variable_chunks(lst):
if lst == []:
return [[]]
result = []
y = 0
for x in range(1, len(lst) + 1):
result.append(lst[y:y+x])
y = y+x
return [x for x in result if x != []]
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(solution.variable_chunks([]), [[]])
test.assert_equals(solution.variable_chunks([1]), [[1]])
test.assert_equals(solution.variable_chunks([1,2]), [[1],[2]])
test.assert_equals(solution.variable_chunks([1, 2, 3, 4]), [[1], [2, 3], [4]])
test.assert_equals(solution.variable_chunks([1, 2, 3, 4, 5, 6]), [[1], [2, 3], [4, 5, 6]])
test.assert_equals(solution.variable_chunks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]), [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15], [16]])
test.assert_equals(solution.variable_chunks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]), [[1], [2, 3], [4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
test.assert_equals(solution.variable_chunks(['a','b','c','d','e']), [['a'], ['b', 'c'],['d','e']])