List Chunks
Imagine splitting a long list into smaller portions, such as showing products page by page or sending data in batches. Write a function chunk that receives a list items and a chunk size size. Split the list into consecutive chunks of that size. The last chunk may be shorter if there are not enough elements left.
Примеры
Ввод
[1, 2, 3, 4, 5], 2Вывод
[[1, 2], [3, 4], [5]]Ввод
[1, 2, 3], 1Вывод
[[1], [2], [3]]Ввод
[], 3Вывод
[]Нужно реализовать
def chunk(items: list, size: int) -> list[list]:
Теги
listsarray
Jelentkezz be a beküldési előzmények megtekintéséhez
BejelentkezésJelentkezz be az MI-mentor használatához
BejelentkezésVisible 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
[1], 5Expected
[[1]]Focus radio
Paused · SomaFM · Fluid