List Batches
Large lists are often easier to process in smaller batches, such as sending data in groups or showing items page by page. Write a function chunk that splits the list arr into sublists with at most size elements each. Keep the original element order. If there are not enough elements to fill the last chunk, it should simply be shorter.
Примеры
Ввод
[1, 2, 3, 4, 5], 2Вывод
[[1, 2], [3, 4], [5]]Ввод
[1, 2, 3], 1Вывод
[[1], [2], [3]]Ввод
[], 3Вывод
[]Нужно реализовать
def chunk(arr: list, size: int) -> list:
Теги
listsarray
Log in om je inzendingsgeschiedenis te zien
InloggenLog in om de AI-mentor te gebruiken
InloggenVisible tests
1
Input
[1,2,3,4,5], 2Expected
[[1,2],[3,4],[5]]2
Input
[1,2,3], 1Expected
[[1],[2],[3]]3
Input
[], 3Expected
[]4
Input
[1,2,3,4], 4Expected
[[1,2,3,4]]5
Input
[1,2,3,4,5,6], 3Expected
[[1,2,3],[4,5,6]]6
Input
[], 10Expected
[]Focus radio
Paused · SomaFM · Fluid