dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
77 lines
1.7 KiB
Markdown
77 lines
1.7 KiB
Markdown
---
|
|
id: 6a2037a68a0bc2aef0006001
|
|
title: "Challenge 339: Array Chunks"
|
|
challengeType: 29
|
|
dashedName: challenge-339
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an array and a chunk size, return the array split into sub-arrays of that size.
|
|
|
|
- The last chunk may be smaller if the array doesn't divide evenly.
|
|
|
|
# --hints--
|
|
|
|
`chunk_array([1, 2, 3, 4, 5, 6], 3)` should return `[[1, 2, 3], [4, 5, 6]]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(chunk_array([1, 2, 3, 4, 5, 6], 3), [[1, 2, 3], [4, 5, 6]])`)
|
|
}})
|
|
```
|
|
|
|
`chunk_array([1, "two", 3, "four", 5, "six", 7, "eight"], 2)` should return `[[1, "two"], [3, "four"], [5, "six"], [7, "eight"]]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(chunk_array([1, "two", 3, "four", 5, "six", 7, "eight"], 2), [[1, "two"], [3, "four"], [5, "six"], [7, "eight"]])`)
|
|
}})
|
|
```
|
|
|
|
`chunk_array([1, 2, 3, 4, 5], 3)` should return `[[1, 2, 3], [4, 5]]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(chunk_array([1, 2, 3, 4, 5], 3), [[1, 2, 3], [4, 5]])`)
|
|
}})
|
|
```
|
|
|
|
`chunk_array(["a", "b", "c", "d", "e"], 1)` should return `[["a"], ["b"], ["c"], ["d"], ["e"]]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(chunk_array(["a", "b", "c", "d", "e"], 1), [["a"], ["b"], ["c"], ["d"], ["e"]])`)
|
|
}})
|
|
```
|
|
|
|
`chunk_array([1, 2, 3], 5)` should return `[[1, 2, 3]]`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(chunk_array([1, 2, 3], 5), [[1, 2, 3]])`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def chunk_array(arr, size):
|
|
|
|
return arr
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def chunk_array(arr, size):
|
|
return [arr[i:i + size] for i in range(0, len(arr), size)]
|
|
```
|