# Create chunks from an array

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 array A, you will need to return array A, for example: if n = 4 and array 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 and A= [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 , so you the shortest bytes of each language will be the winner.

• If n is greater than the length of A we need to return A‽ Are you sure you don't mean [A]? – Adám Mar 6 '19 at 13:24
• @chaugiang I still think a too large n should return [A], e.g [[1,2,3]]. What if n is exactly the length of A? – Adám Mar 6 '19 at 13:42
• @chaugiang Adam is correct imo. The return value should be consistent. – Jonah Mar 6 '19 at 16:02
• @chaugiang Can n ever equal 1? – James Mar 6 '19 at 19:41
• In a strongly typed language, it's simply impossible to return A rather than [A] , which would exclude an awful lot of languages. – dfeuer Mar 6 '19 at 22:48

# Wolfram Language (Mathematica), 20 bytes

#2~Partition~UpTo@#&

Try it online!

# Perl 5, 42 bytes

$p=<>-1;s/(,.*?){$p}\K,/],[/g&&($_="[$_]")

Try it online!

• instead of &&(...) you can do ;... - this will save 3 bytes – mik Mar 9 '19 at 5:41
• also, you can embed expression in regexp using @{[...]}, so writing @{[<>-1]} instead of \$p and calculating it will save another byte – mik Mar 9 '19 at 5:50
• hmm, I understand why you did this && (not to put extra brackets when n is at least the length of a), but even the accepted answer does not do that – mik Mar 9 '19 at 6:21

# Red, 46 bytes

func[n l][until[print take/part l n empty? l]]

Try it online!

# R, 49 36 bytes

function(A,n)split(A,(seq(A)-1)%/%n)

Try it online!

Thanks to Kirill L. for the golf.

• @KirillL. of course. Thanks! – Giuseppe Mar 7 '19 at 14:08

# Python 3, 49 bytes

f=lambda x,n:[x[i:i+n]for i in range(0,len(x),n)]

Simple anonymous function implementation with a list comprehension. 47 bytes if you don't count the f= assignment.

Try it online!

# APL(NARS), 55 char, 110 bytes

{⍺≤1:,¨⍵⋄z←¯1↓v←,/(k←1+⌊⍺÷⍨≢⍵)⍺⍴⍵⋄0=r←⍺∣≢⍵:z⋄z,⊂r↑↑k⌷v}

this appear to return the right results... test:

f←{⍺≤1:,¨⍵⋄z←¯1↓v←,/(k←1+⌊⍺÷⍨≢⍵)⍺⍴⍵⋄0=r←⍺∣≢⍵:z⋄z,⊂r↑↑k⌷v}
o 1 f 1 2 3 4 5 6
┌6────────────────────────────┐
│┌1─┐ ┌1─┐ ┌1─┐ ┌1─┐ ┌1─┐ ┌1─┐│
││ 1│ │ 2│ │ 3│ │ 4│ │ 5│ │ 6││
│└~─┘ └~─┘ └~─┘ └~─┘ └~─┘ └~─┘2
└∊────────────────────────────┘
2 f 1 2 3 4 5 6
1 2  3 4  5 6
3 f 1 2 3 4 5 6
1 2 3  4 5 6
4 f 1 2 3 4 5 6
1 2 3 4  5 6
5 f 1 2 3 4 5 6
1 2 3 4 5  6
6 f 1 2 3 4 5 6
1 2 3 4 5 6
7 f 1 2 3 4 5 6
1 2 3 4 5 6

# Kotlin, 19 bytes

{a,n->a.chunked(n)}

o/ built-ins

Try it online!

Input is a series of numbers on stdin. The first number is n, and the rest of the numbers make up the array. Output consists of n comma-separated numbers per line, so each line is a chunk.

~Ilj~#l,'H!t<
vuj{ oN @j<c
>:&}>d2(nOl

# Example

In: 2 1 2 3 4 5 6
Out:
1,2
3,4
5,6

In: 4 1 2 3 4 5 6
Out:
1,2,3,4
5,6,

Note there will be a trailing comma if the last chunk is not the right size. Hopefully this is ok.

Try it online!

# GolfScript, 2 bytes

GS has a built-in. Unfortunately the implicit input got in the way.

~/

Try it online!