Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Uncategorized category for Chunk by dig
import contextlib
from typing import Iterable
def chunking_by(items: list, size: int) -> Iterable:
llista=[]
if not items: return []
while len(items)>0:
llista2=[]
with contextlib.suppress(IndexError):
for i in range(size):
llista2.append(items.pop(0))
llista.append(llista2)
return llista
print("Example:")
print(list(chunking_by([5, 4, 7, 3, 4, 5, 4], 3)))
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("The mission is done! Click 'Check Solution' to earn rewards!")
April 22, 2023
Comments: