Your task is to write a program which given an array and a number, you need to split the array into chunks with size is number.
Rules
Your program will receive an array A
, as well as a positive integer n
. The array should then be split into chunks of length n
, if the length of the string isn't divisible by n
any leftover at the end should be considered its own chunk.
If
n
is greater than length of arrayA
, you will need to return arrayA
, for example: ifn = 4
andarray A = [1,2,3]
, you should return[1,2,3]
The array can contain any type rather than number.
You should not change order (or direction) of any item from left to right. For example
if n = 2
andA= [1,2,3]
. Any result rather than[[1,2],[3]]
will be invalid.
Test Cases
n A Output
2 [1,2,3,4,5,6] [[1,2],[3,4],[5,6]]
3 [1,2,3,4,5,6] [[1,2,3],[4,5,6]]
4 [1,2,3,4,5,6] [[1,2,3,4],[5,6]]
This is code-golf, so you the shortest bytes of each language will be the winner.
n
is greater than the length ofA
we need to returnA
‽ Are you sure you don't mean[A]
? \$\endgroup\$n
should return[A]
, e.g[[1,2,3]]
. What ifn
is exactly the length ofA
? \$\endgroup\$A
rather than[A]
, which would exclude an awful lot of languages. \$\endgroup\$