Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Chunk by Szena
from typing import Iterable
def chunking_by(items: list, size: int) -> Iterable:
# your code here
if not items:
return []
o = len(items) % size
l = len(items) // size
i = 1
last = 0
r = []
while i <= l:
r.append(items[last:i * size])
last = i * size
i += 1
if o:
r.append(items[last:])
print(r)
return r
if __name__ == '__main__':
# These "asserts" are used for self-checking and not for an auto-testing
assert list(chunking_by([5, 4, 7, 3, 4, 5, 4], 3)) == [[5, 4, 7], [3, 4, 5], [4]]
assert list(chunking_by([3, 4, 5], 1)) == [[3], [4], [5]]
assert list(chunking_by([5, 4], 7)) == [[5, 4]]
assert list(chunking_by([], 3)) == []
assert list(chunking_by([4, 4, 4, 4], 4)) == [[4, 4, 4, 4]]
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 8, 2021