22
\$\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
  • 5
    \$\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
    Commented Mar 6, 2019 at 13:24
  • 10
    \$\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
    Commented Mar 6, 2019 at 13:42
  • 5
    \$\begingroup\$ @chaugiang Adam is correct imo. The return value should be consistent. \$\endgroup\$
    – Jonah
    Commented Mar 6, 2019 at 16:02
  • 1
    \$\begingroup\$ @chaugiang Can n ever equal 1? \$\endgroup\$
    – DJMcMayhem
    Commented Mar 6, 2019 at 19:41
  • 6
    \$\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
    Commented Mar 6, 2019 at 22:48

43 Answers 43

1
2
3
\$\begingroup\$

Jelly, 1 byte

s

Try it online!

While the printer makes it look like single-element splits are not wrapped into lists, they actually are.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ This night give a better output as to showing that single element arrays are still actually arrays. \$\endgroup\$ Commented Mar 6, 2019 at 13:33
  • \$\begingroup\$ Er, is the downvote because I didn’t add @Nick Kennedy’s link? \$\endgroup\$
    – Ven
    Commented Mar 7, 2019 at 7:48
  • \$\begingroup\$ certainly not from me \$\endgroup\$ Commented Mar 7, 2019 at 7:49
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
    Commented 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
    Commented 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
    Commented 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\$

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\$
2
\$\begingroup\$

Factor, 5 bytes

group

Try it online!

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

Vyxal, 1 byte

Try it Online!

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

Ruby, 17 bytes

proc &:each_slice

Try it online!

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

GolfScript, 2 bytes

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

~/

Try it online!

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

Thunno 2, 1 byte

Try it online!

Yeah, another built-in :p

Polyglots with Vyxal apparently...

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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