#212 · Lists · Beginner · 30 XP · about 5 min
A packing machine fills boxes in order, size items per box, and the last box may be only partly full. Write chunk(items, size) that returns the boxes as a list of lists.
Every box except the last holds exactly size items, and the items keep their order. An empty list gives an empty list: no boxes at all.
chunk([1, 2, 3, 4, 5], 2) # returns [[1, 2], [3, 4], [5]] chunk(['a', 'b', 'c', 'd', 'e', 'f'], 3) # returns [['a', 'b', 'c'], ['d', 'e', 'f']] chunk([1, 2], 5) # returns [[1, 2]]
size is always at least 1.
First solves only, ranked by stars and then time.