21
\$\begingroup\$

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.

\$\endgroup\$
12
  • 4
    \$\begingroup\$ If n is greater than the length of A we need to return A‽ Are you sure you don't mean [A]? \$\endgroup\$
    – Adám
    Mar 6, 2019 at 13:24
  • 9
    \$\begingroup\$ @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? \$\endgroup\$
    – Adám
    Mar 6, 2019 at 13:42
  • 4
    \$\begingroup\$ @chaugiang Adam is correct imo. The return value should be consistent. \$\endgroup\$
    – Jonah
    Mar 6, 2019 at 16:02
  • 1
    \$\begingroup\$ @chaugiang Can n ever equal 1? \$\endgroup\$
    – DJMcMayhem
    Mar 6, 2019 at 19:41
  • 4
    \$\begingroup\$ In a strongly typed language, it's simply impossible to return A rather than [A] , which would exclude an awful lot of languages. \$\endgroup\$
    – dfeuer
    Mar 6, 2019 at 22:48

39 Answers 39

1
2
2
\$\begingroup\$

Wolfram Language (Mathematica), 20 bytes

#2~Partition~UpTo@#&

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 42 bytes

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ instead of &&(...) you can do ;... - this will save 3 bytes \$\endgroup\$
    – mik
    Mar 9, 2019 at 5:41
  • \$\begingroup\$ also, you can embed expression in regexp using @{[...]}, so writing @{[<>-1]} instead of $p and calculating it will save another byte \$\endgroup\$
    – mik
    Mar 9, 2019 at 5:50
  • \$\begingroup\$ 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 \$\endgroup\$
    – mik
    Mar 9, 2019 at 6:21
2
\$\begingroup\$

Red, 46 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

R, 49 36 bytes

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

Try it online!

Thanks to Kirill L. for the golf.

\$\endgroup\$
2
  • \$\begingroup\$ How about this, for 36 \$\endgroup\$
    – Kirill L.
    Mar 7, 2019 at 8:27
  • \$\begingroup\$ @KirillL. of course. Thanks! \$\endgroup\$
    – Giuseppe
    Mar 7, 2019 at 14:08
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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 
\$\endgroup\$
1
\$\begingroup\$

Kotlin, 19 bytes

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

o/ built-ins

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ahead, 38 bytes

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!

\$\endgroup\$
0
\$\begingroup\$

GolfScript, 2 bytes

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

~/

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.