Create chunks from an array
$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
nis greater than length of arrayA, you will need to return arrayA, for example: ifn = 4andarray 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 = 2andA= [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.
code-golf array-manipulation
$endgroup$
|
show 5 more comments
$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
nis greater than length of arrayA, you will need to return arrayA, for example: ifn = 4andarray 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 = 2andA= [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.
code-golf array-manipulation
$endgroup$
4
$begingroup$
Ifnis greater than the length ofAwe need to returnA‽ Are you sure you don't mean[A]?
$endgroup$
– Adám
Mar 6 at 13:24
9
$begingroup$
@chaugiang I still think a too largenshould return[A], e.g[[1,2,3]]. What ifnis exactly the length ofA?
$endgroup$
– Adám
Mar 6 at 13:42
4
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
1
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
3
$begingroup$
In a strongly typed language, it's simply impossible to returnArather than[A], which would exclude an awful lot of languages.
$endgroup$
– dfeuer
Mar 6 at 22:48
|
show 5 more comments
$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
nis greater than length of arrayA, you will need to return arrayA, for example: ifn = 4andarray 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 = 2andA= [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.
code-golf array-manipulation
$endgroup$
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
nis greater than length of arrayA, you will need to return arrayA, for example: ifn = 4andarray 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 = 2andA= [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.
code-golf array-manipulation
code-golf array-manipulation
edited Mar 6 at 13:37
chau giang
asked Mar 6 at 11:34
chau giangchau giang
12419
12419
4
$begingroup$
Ifnis greater than the length ofAwe need to returnA‽ Are you sure you don't mean[A]?
$endgroup$
– Adám
Mar 6 at 13:24
9
$begingroup$
@chaugiang I still think a too largenshould return[A], e.g[[1,2,3]]. What ifnis exactly the length ofA?
$endgroup$
– Adám
Mar 6 at 13:42
4
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
1
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
3
$begingroup$
In a strongly typed language, it's simply impossible to returnArather than[A], which would exclude an awful lot of languages.
$endgroup$
– dfeuer
Mar 6 at 22:48
|
show 5 more comments
4
$begingroup$
Ifnis greater than the length ofAwe need to returnA‽ Are you sure you don't mean[A]?
$endgroup$
– Adám
Mar 6 at 13:24
9
$begingroup$
@chaugiang I still think a too largenshould return[A], e.g[[1,2,3]]. What ifnis exactly the length ofA?
$endgroup$
– Adám
Mar 6 at 13:42
4
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
1
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
3
$begingroup$
In a strongly typed language, it's simply impossible to returnArather than[A], which would exclude an awful lot of languages.
$endgroup$
– dfeuer
Mar 6 at 22:48
4
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 at 13:24
$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 at 13:24
9
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 at 13:42
$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 at 13:42
4
4
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
1
1
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
3
3
$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 at 22:48
$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 at 22:48
|
show 5 more comments
37 Answers
37
active
oldest
votes
1 2
next
$begingroup$
05AB1E, 1 byte
ô
Try it online or verify all test cases.
Builtins ftw. :)
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 36 bytes
Takes input as (n)(array).
n=>g=a=>a+a&&[a.splice(0,n),...g(a)]
Try it online!
Commented
n => // n = chunk size
g = a => // g = recursive function taking the array a
a + a // if a is empty, stop recursion and return an empty string
&& // otherwise, return an array made of:
[ a.splice(0, n), // the next chunk
...g(a) // followed by the result of a recursive call
] // (the last call leads to ...'', which adds nothing)
$endgroup$
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 bytesSBCS
⊢⊂⍨(⍴⊢)⍴1↑⍨⊣
Big thanks to Adám for basically doing basically all the golfing (and for basically all the APL knowledge I have currently >_>).
Explanation
⊂⍨ Partitioned enclose (commuted, i.e. left and right switched) - for each ⍵ in left, ⍺ in right, if ⍺ = 0, create a new sub-array, push ⍵ to latest sub-array
⊢ Right argument of entire expression
⍴ Reshape - Change size of right into dimensions specified by left
(⍴ ) Shape of (here, there is only one dimension - length)
⊢ Right argument of entire expression
↑⍨ Take (commuted) - takes ⍺ elements from left where ⍺ is right. Extra elements (zeroes here) are automatically added
1 1
⊣ Left argument of entire expression
Execution
Arguments 2, 1 2 3 4 5 6 7. Note that APL arrays are of the form a b c, with optional surrounding parentheses.
⊣ 2
1 1
↑⍨ 1↑2 = 1 0
⊢ 1 2 3 4 5 6 7
(⍴ ) ⍴1 2 3 4 5 6 7 = 7
⍴ 7⍴1 0 = 1 0 1 0 1 0 1
⊢ 1 2 3 4 5 6 7
⊂⍨ 1 0 1 0 1 0 1⊂1 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)
Try it online!
$endgroup$
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
add a comment |
$begingroup$
Python 3, 61 bytes
lambda A,n:[A,[A[x:x+n]for x in range(0,len(A),n)]][n<len(A)]
Try it online!
Modifies Henry T's existing Python 3 solution to produce valid output for n >= len(A).
Posting as its own answer due to lack of commenting privileges.
$endgroup$
add a comment |
$begingroup$
Prolog (SWI), 90 84 61 bytes
Code:
*_*.
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=.
The input format might be a bit weird, but it is:
A * n * Result.
For example, for the input:
n = 2
A = [1, 2, 3, 4, 5, 6]
You would need to use [1, 2, 3, 4, 5, 6] * 2 * Result..
Try it online!
Ungolfed version:
divide(, _, ).
divide(List, N, [Prefix | Result]) :-
length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result)
; Prefix = List, Result = .
Try it online!.
$endgroup$
add a comment |
$begingroup$
PHP, 15 bytes
$f=array_chunk;
requires PHP 7. Call with $f(ARRAY, N).
$endgroup$
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
add a comment |
$begingroup$
Perl 6, 13 bytes
{*.batch($_)}
Try it online!
Curried function wrapping the batch built-in.
$endgroup$
$begingroup$
Did they fix that bug where you had to have a;after the Whatever code?
$endgroup$
– Jo King
Mar 6 at 22:44
2
$begingroup$
@JoKing No. You only need a;when passing arguments with$^aor@_.
$endgroup$
– nwellnhof
Mar 7 at 1:18
add a comment |
$begingroup$
Clean, 54 bytes
import StdEnv
$n l=[l%(i,i+n-1)\i<-[0,n..length l-1]]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 39 bytes
i,j=input()
while j:print j[:i];j=j[i:]
Try it online!
Assumes that 1 chunk per line is acceptable output.
$endgroup$
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
add a comment |
$begingroup$
Brainfuck, 71 bytes
,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]
Dunno if this counts or not... input format:
<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
1234567890123492034
n is 32 since the ASCII value of space is 32
Takes the input and puts in a space every time n characters pass
Explanation (no commas because that would break the program):
, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again
$endgroup$
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use,more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.
$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I haven n n A spaceas my cell setup, if you can think of a better way...
$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
CouldA space n n n ...work (orspace A n n n...)?
$endgroup$
– MilkyWay90
Mar 8 at 2:50
|
show 2 more comments
$begingroup$
Python 3, 46 chars
lambda A,n:[A[:n],*(f(A[n:],n)if A[n:]else)]
-1 thanks to @Collin Phillips.
Try it online!
$endgroup$
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
add a comment |
$begingroup$
CJam, 3 bytes
{/}
This is an anonymous block that takes an array of numbers and a number from the stack, and replaces them by an array of arrays.
Try it online!
$endgroup$
add a comment |
$begingroup$
Brachylog, 2 bytes
ġ₎
Try it online!
$endgroup$
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
add a comment |
$begingroup$
Elixir, 16 bytes
Enum.chunk_every
Try it online!
$endgroup$
add a comment |
$begingroup$
Charcoal, 1 byte
⪪
Try it online! Charcoal's default I/O makes it difficult to demonstrate using anything except strings. If you want a full program that takes numeric lists and outputs formatted lists then this can be done as follows:
E⪪AN⪫ι,
Try it online! Link is to verbose version of code. Explanation:
A Input array
⪪ Split into chunks of
N Input number
E Map over chunks
ι Current chunk
⪫ Joined with
, Literal `,`
Implicitly print each chunk on its own line
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 78 77 43 bytes
a=>b=>{int i=0;return a.GroupBy(_=>i++/b);}
Try it online!
I think we should be able to just write int i; because 0 is the default of int. I let it to avoid the error: error CS0165: Use of unassigned local variable 'i'.
$endgroup$
add a comment |
$begingroup$
F# (.NET Core), 15 bytes
Seq.chunkBySize
Try it online!
Well F# has a builtin...
$endgroup$
add a comment |
$begingroup$
J, 4 bytes
<~-
Try it online!
Takes the array as left arg and chunk size as right arg.
Uses a dyadic hook and the infix adverb with a negative argument, which does what we want by definition.
Note: The return type must be boxed because J only allows tables of equal sized items.
$endgroup$
add a comment |
$begingroup$
Japt, 2 bytes
òV
Try it online!
$endgroup$
add a comment |
$begingroup$
PHP, 45 bytes
function f($a,$b){return array_chunk($a,$b);}
Try it online!
$endgroup$
3
$begingroup$
Would justarray_chunkbe a valid answer?
$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
add a comment |
$begingroup$
Java 10, 106 80 bytes
L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}
Prints the chunks without delimiter.
Try it online.
106 bytes:
L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}
Actually returns a list of lists.
Try it online.
Explanation:
L->n->{ // Method with List and integer parameters and List return-type
var r=new java.util.Stack();// Create an empty List
for(int l=L.size(), // Determine the size of the input-List
i=0;i<l;) // Loop `i` in the range [0, size):
r.add( // Add to the result-List:
L.subList(i, // A sublist of the input-list in the range from `i`
Math.min(i+=n,l))); // to the minimum of: `i` + input-integer or the size
// (and increase `i` by the input-integer at the same)
return r;} // Return the List of Lists of integers as result
$endgroup$
add a comment |
$begingroup$
K (oK), 10 bytes
{(0N,x)#y}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n,a{[*a.each_slice(n)]}
Try it online!
If we can return enumerators instead of arrays, then it becomes simply:
Ruby, 21 bytes
->n,a{a.each_slice n}
Try it online!
$endgroup$
add a comment |
$begingroup$
PicoLisp, 75 74 bytes
(de f(n l)(if(>= n(length l))(list l)(cons(head n l)(f n(tail(- 0 n)l)))))
Try it online!
$endgroup$
add a comment |
$begingroup$
Coconut, 8 bytes
groupsof
Try it online!
$endgroup$
add a comment |
$begingroup$
V, 6 bytes
òÀf,r
Try it online!
Hexdump:
00000000: f2c0 662c 720a ..f,r.
Explanation:
ò " Until an error happens:
f " (f)ind the...
À " n'th...
, " ","
" (If there are less than n commas after the cursor, throw an error)
r " Replace the char under the cursor with...
<cr> " A newline
$endgroup$
add a comment |
$begingroup$
Clojure, 14 bytes
#(partition %)
builtins I guess
$endgroup$
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
add a comment |
$begingroup$
Haskell, 26 bytes
import Data.Lists
chunksOf
Here's a more interesting version, with just a few more bytes (thanks to nimi for five bytes in each solution):
Haskell, 31 bytes
n!=
n!x=take n x:n!drop n x
Try it online!
$endgroup$
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
$begingroup$
n!x=take n x:n!drop n x.Data.Listsprovides alsochunksOf.
$endgroup$
– nimi
Mar 6 at 23:00
add a comment |
$begingroup$
PowerShell, 67 65 bytes
-2 bytes thanks AdmBorkBork
param($n,$a)$a|%{$b+=,$_
if($b.Count-ge$n){,$b;rv b}}
if($b){,$b}
Try it online!
$endgroup$
2
$begingroup$
You should be able torv b(alias forRemove-Variable) instead of$b=@()to save two bytes.
$endgroup$
– AdmBorkBork
Mar 6 at 20:45
add a comment |
$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$
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
add a comment |
1 2
next
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180970%2fcreate-chunks-from-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
37 Answers
37
active
oldest
votes
37 Answers
37
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
$begingroup$
05AB1E, 1 byte
ô
Try it online or verify all test cases.
Builtins ftw. :)
$endgroup$
add a comment |
$begingroup$
05AB1E, 1 byte
ô
Try it online or verify all test cases.
Builtins ftw. :)
$endgroup$
add a comment |
$begingroup$
05AB1E, 1 byte
ô
Try it online or verify all test cases.
Builtins ftw. :)
$endgroup$
05AB1E, 1 byte
ô
Try it online or verify all test cases.
Builtins ftw. :)
answered Mar 6 at 11:38
Kevin CruijssenKevin Cruijssen
42.6k571217
42.6k571217
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 36 bytes
Takes input as (n)(array).
n=>g=a=>a+a&&[a.splice(0,n),...g(a)]
Try it online!
Commented
n => // n = chunk size
g = a => // g = recursive function taking the array a
a + a // if a is empty, stop recursion and return an empty string
&& // otherwise, return an array made of:
[ a.splice(0, n), // the next chunk
...g(a) // followed by the result of a recursive call
] // (the last call leads to ...'', which adds nothing)
$endgroup$
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
add a comment |
$begingroup$
JavaScript (ES6), 36 bytes
Takes input as (n)(array).
n=>g=a=>a+a&&[a.splice(0,n),...g(a)]
Try it online!
Commented
n => // n = chunk size
g = a => // g = recursive function taking the array a
a + a // if a is empty, stop recursion and return an empty string
&& // otherwise, return an array made of:
[ a.splice(0, n), // the next chunk
...g(a) // followed by the result of a recursive call
] // (the last call leads to ...'', which adds nothing)
$endgroup$
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
add a comment |
$begingroup$
JavaScript (ES6), 36 bytes
Takes input as (n)(array).
n=>g=a=>a+a&&[a.splice(0,n),...g(a)]
Try it online!
Commented
n => // n = chunk size
g = a => // g = recursive function taking the array a
a + a // if a is empty, stop recursion and return an empty string
&& // otherwise, return an array made of:
[ a.splice(0, n), // the next chunk
...g(a) // followed by the result of a recursive call
] // (the last call leads to ...'', which adds nothing)
$endgroup$
JavaScript (ES6), 36 bytes
Takes input as (n)(array).
n=>g=a=>a+a&&[a.splice(0,n),...g(a)]
Try it online!
Commented
n => // n = chunk size
g = a => // g = recursive function taking the array a
a + a // if a is empty, stop recursion and return an empty string
&& // otherwise, return an array made of:
[ a.splice(0, n), // the next chunk
...g(a) // followed by the result of a recursive call
] // (the last call leads to ...'', which adds nothing)
edited Mar 6 at 12:17
answered Mar 6 at 11:41
ArnauldArnauld
80.7k797334
80.7k797334
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
add a comment |
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
$begingroup$
Now that is a neat and clean solution, and I learned about recursive anonymous functions too!
$endgroup$
– Joe the Person
Mar 7 at 17:44
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 bytesSBCS
⊢⊂⍨(⍴⊢)⍴1↑⍨⊣
Big thanks to Adám for basically doing basically all the golfing (and for basically all the APL knowledge I have currently >_>).
Explanation
⊂⍨ Partitioned enclose (commuted, i.e. left and right switched) - for each ⍵ in left, ⍺ in right, if ⍺ = 0, create a new sub-array, push ⍵ to latest sub-array
⊢ Right argument of entire expression
⍴ Reshape - Change size of right into dimensions specified by left
(⍴ ) Shape of (here, there is only one dimension - length)
⊢ Right argument of entire expression
↑⍨ Take (commuted) - takes ⍺ elements from left where ⍺ is right. Extra elements (zeroes here) are automatically added
1 1
⊣ Left argument of entire expression
Execution
Arguments 2, 1 2 3 4 5 6 7. Note that APL arrays are of the form a b c, with optional surrounding parentheses.
⊣ 2
1 1
↑⍨ 1↑2 = 1 0
⊢ 1 2 3 4 5 6 7
(⍴ ) ⍴1 2 3 4 5 6 7 = 7
⍴ 7⍴1 0 = 1 0 1 0 1 0 1
⊢ 1 2 3 4 5 6 7
⊂⍨ 1 0 1 0 1 0 1⊂1 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)
Try it online!
$endgroup$
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 bytesSBCS
⊢⊂⍨(⍴⊢)⍴1↑⍨⊣
Big thanks to Adám for basically doing basically all the golfing (and for basically all the APL knowledge I have currently >_>).
Explanation
⊂⍨ Partitioned enclose (commuted, i.e. left and right switched) - for each ⍵ in left, ⍺ in right, if ⍺ = 0, create a new sub-array, push ⍵ to latest sub-array
⊢ Right argument of entire expression
⍴ Reshape - Change size of right into dimensions specified by left
(⍴ ) Shape of (here, there is only one dimension - length)
⊢ Right argument of entire expression
↑⍨ Take (commuted) - takes ⍺ elements from left where ⍺ is right. Extra elements (zeroes here) are automatically added
1 1
⊣ Left argument of entire expression
Execution
Arguments 2, 1 2 3 4 5 6 7. Note that APL arrays are of the form a b c, with optional surrounding parentheses.
⊣ 2
1 1
↑⍨ 1↑2 = 1 0
⊢ 1 2 3 4 5 6 7
(⍴ ) ⍴1 2 3 4 5 6 7 = 7
⍴ 7⍴1 0 = 1 0 1 0 1 0 1
⊢ 1 2 3 4 5 6 7
⊂⍨ 1 0 1 0 1 0 1⊂1 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)
Try it online!
$endgroup$
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
add a comment |
$begingroup$
APL (Dyalog Unicode), 12 bytesSBCS
⊢⊂⍨(⍴⊢)⍴1↑⍨⊣
Big thanks to Adám for basically doing basically all the golfing (and for basically all the APL knowledge I have currently >_>).
Explanation
⊂⍨ Partitioned enclose (commuted, i.e. left and right switched) - for each ⍵ in left, ⍺ in right, if ⍺ = 0, create a new sub-array, push ⍵ to latest sub-array
⊢ Right argument of entire expression
⍴ Reshape - Change size of right into dimensions specified by left
(⍴ ) Shape of (here, there is only one dimension - length)
⊢ Right argument of entire expression
↑⍨ Take (commuted) - takes ⍺ elements from left where ⍺ is right. Extra elements (zeroes here) are automatically added
1 1
⊣ Left argument of entire expression
Execution
Arguments 2, 1 2 3 4 5 6 7. Note that APL arrays are of the form a b c, with optional surrounding parentheses.
⊣ 2
1 1
↑⍨ 1↑2 = 1 0
⊢ 1 2 3 4 5 6 7
(⍴ ) ⍴1 2 3 4 5 6 7 = 7
⍴ 7⍴1 0 = 1 0 1 0 1 0 1
⊢ 1 2 3 4 5 6 7
⊂⍨ 1 0 1 0 1 0 1⊂1 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)
Try it online!
$endgroup$
APL (Dyalog Unicode), 12 bytesSBCS
⊢⊂⍨(⍴⊢)⍴1↑⍨⊣
Big thanks to Adám for basically doing basically all the golfing (and for basically all the APL knowledge I have currently >_>).
Explanation
⊂⍨ Partitioned enclose (commuted, i.e. left and right switched) - for each ⍵ in left, ⍺ in right, if ⍺ = 0, create a new sub-array, push ⍵ to latest sub-array
⊢ Right argument of entire expression
⍴ Reshape - Change size of right into dimensions specified by left
(⍴ ) Shape of (here, there is only one dimension - length)
⊢ Right argument of entire expression
↑⍨ Take (commuted) - takes ⍺ elements from left where ⍺ is right. Extra elements (zeroes here) are automatically added
1 1
⊣ Left argument of entire expression
Execution
Arguments 2, 1 2 3 4 5 6 7. Note that APL arrays are of the form a b c, with optional surrounding parentheses.
⊣ 2
1 1
↑⍨ 1↑2 = 1 0
⊢ 1 2 3 4 5 6 7
(⍴ ) ⍴1 2 3 4 5 6 7 = 7
⍴ 7⍴1 0 = 1 0 1 0 1 0 1
⊢ 1 2 3 4 5 6 7
⊂⍨ 1 0 1 0 1 0 1⊂1 2 3 4 5 6 7 = (1 2)(3 4)(5 6)(7)
Try it online!
answered Mar 6 at 13:04
ASCII-onlyASCII-only
4,4701338
4,4701338
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
add a comment |
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
7
7
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
$begingroup$
Congratulations on your first APL answer. And nicely explained too! Here, have an APL pie: 🥧
$endgroup$
– Adám
Mar 6 at 13:08
add a comment |
$begingroup$
Python 3, 61 bytes
lambda A,n:[A,[A[x:x+n]for x in range(0,len(A),n)]][n<len(A)]
Try it online!
Modifies Henry T's existing Python 3 solution to produce valid output for n >= len(A).
Posting as its own answer due to lack of commenting privileges.
$endgroup$
add a comment |
$begingroup$
Python 3, 61 bytes
lambda A,n:[A,[A[x:x+n]for x in range(0,len(A),n)]][n<len(A)]
Try it online!
Modifies Henry T's existing Python 3 solution to produce valid output for n >= len(A).
Posting as its own answer due to lack of commenting privileges.
$endgroup$
add a comment |
$begingroup$
Python 3, 61 bytes
lambda A,n:[A,[A[x:x+n]for x in range(0,len(A),n)]][n<len(A)]
Try it online!
Modifies Henry T's existing Python 3 solution to produce valid output for n >= len(A).
Posting as its own answer due to lack of commenting privileges.
$endgroup$
Python 3, 61 bytes
lambda A,n:[A,[A[x:x+n]for x in range(0,len(A),n)]][n<len(A)]
Try it online!
Modifies Henry T's existing Python 3 solution to produce valid output for n >= len(A).
Posting as its own answer due to lack of commenting privileges.
edited Mar 6 at 16:34
answered Mar 6 at 16:12
squidsquid
1114
1114
add a comment |
add a comment |
$begingroup$
Prolog (SWI), 90 84 61 bytes
Code:
*_*.
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=.
The input format might be a bit weird, but it is:
A * n * Result.
For example, for the input:
n = 2
A = [1, 2, 3, 4, 5, 6]
You would need to use [1, 2, 3, 4, 5, 6] * 2 * Result..
Try it online!
Ungolfed version:
divide(, _, ).
divide(List, N, [Prefix | Result]) :-
length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result)
; Prefix = List, Result = .
Try it online!.
$endgroup$
add a comment |
$begingroup$
Prolog (SWI), 90 84 61 bytes
Code:
*_*.
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=.
The input format might be a bit weird, but it is:
A * n * Result.
For example, for the input:
n = 2
A = [1, 2, 3, 4, 5, 6]
You would need to use [1, 2, 3, 4, 5, 6] * 2 * Result..
Try it online!
Ungolfed version:
divide(, _, ).
divide(List, N, [Prefix | Result]) :-
length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result)
; Prefix = List, Result = .
Try it online!.
$endgroup$
add a comment |
$begingroup$
Prolog (SWI), 90 84 61 bytes
Code:
*_*.
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=.
The input format might be a bit weird, but it is:
A * n * Result.
For example, for the input:
n = 2
A = [1, 2, 3, 4, 5, 6]
You would need to use [1, 2, 3, 4, 5, 6] * 2 * Result..
Try it online!
Ungolfed version:
divide(, _, ).
divide(List, N, [Prefix | Result]) :-
length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result)
; Prefix = List, Result = .
Try it online!.
$endgroup$
Prolog (SWI), 90 84 61 bytes
Code:
*_*.
L*N*[P|R]:-length(P,N),append(P,T,L),T*N*R;P=L,R=.
The input format might be a bit weird, but it is:
A * n * Result.
For example, for the input:
n = 2
A = [1, 2, 3, 4, 5, 6]
You would need to use [1, 2, 3, 4, 5, 6] * 2 * Result..
Try it online!
Ungolfed version:
divide(, _, ).
divide(List, N, [Prefix | Result]) :-
length(Prefix, N), append(Prefix, Remaining, List), divide(Remaining, N, Result)
; Prefix = List, Result = .
Try it online!.
edited Mar 6 at 16:09
answered Mar 6 at 13:27
AdnanAdnan
36k562226
36k562226
add a comment |
add a comment |
$begingroup$
PHP, 15 bytes
$f=array_chunk;
requires PHP 7. Call with $f(ARRAY, N).
$endgroup$
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
add a comment |
$begingroup$
PHP, 15 bytes
$f=array_chunk;
requires PHP 7. Call with $f(ARRAY, N).
$endgroup$
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
add a comment |
$begingroup$
PHP, 15 bytes
$f=array_chunk;
requires PHP 7. Call with $f(ARRAY, N).
$endgroup$
PHP, 15 bytes
$f=array_chunk;
requires PHP 7. Call with $f(ARRAY, N).
answered Mar 6 at 13:26
TitusTitus
13.4k11240
13.4k11240
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
add a comment |
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
5
5
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
I don't think you're required to give another name to a builtin, so this just scores 11, doesn't it?
$endgroup$
– Neil
Mar 6 at 16:12
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
$begingroup$
@Neil I thought that might be a forbidden loophole; but you may be right.
$endgroup$
– Titus
Mar 8 at 15:22
add a comment |
$begingroup$
Perl 6, 13 bytes
{*.batch($_)}
Try it online!
Curried function wrapping the batch built-in.
$endgroup$
$begingroup$
Did they fix that bug where you had to have a;after the Whatever code?
$endgroup$
– Jo King
Mar 6 at 22:44
2
$begingroup$
@JoKing No. You only need a;when passing arguments with$^aor@_.
$endgroup$
– nwellnhof
Mar 7 at 1:18
add a comment |
$begingroup$
Perl 6, 13 bytes
{*.batch($_)}
Try it online!
Curried function wrapping the batch built-in.
$endgroup$
$begingroup$
Did they fix that bug where you had to have a;after the Whatever code?
$endgroup$
– Jo King
Mar 6 at 22:44
2
$begingroup$
@JoKing No. You only need a;when passing arguments with$^aor@_.
$endgroup$
– nwellnhof
Mar 7 at 1:18
add a comment |
$begingroup$
Perl 6, 13 bytes
{*.batch($_)}
Try it online!
Curried function wrapping the batch built-in.
$endgroup$
Perl 6, 13 bytes
{*.batch($_)}
Try it online!
Curried function wrapping the batch built-in.
edited Mar 7 at 1:32
answered Mar 6 at 13:54
nwellnhofnwellnhof
7,44011128
7,44011128
$begingroup$
Did they fix that bug where you had to have a;after the Whatever code?
$endgroup$
– Jo King
Mar 6 at 22:44
2
$begingroup$
@JoKing No. You only need a;when passing arguments with$^aor@_.
$endgroup$
– nwellnhof
Mar 7 at 1:18
add a comment |
$begingroup$
Did they fix that bug where you had to have a;after the Whatever code?
$endgroup$
– Jo King
Mar 6 at 22:44
2
$begingroup$
@JoKing No. You only need a;when passing arguments with$^aor@_.
$endgroup$
– nwellnhof
Mar 7 at 1:18
$begingroup$
Did they fix that bug where you had to have a
; after the Whatever code?$endgroup$
– Jo King
Mar 6 at 22:44
$begingroup$
Did they fix that bug where you had to have a
; after the Whatever code?$endgroup$
– Jo King
Mar 6 at 22:44
2
2
$begingroup$
@JoKing No. You only need a
; when passing arguments with $^a or @_.$endgroup$
– nwellnhof
Mar 7 at 1:18
$begingroup$
@JoKing No. You only need a
; when passing arguments with $^a or @_.$endgroup$
– nwellnhof
Mar 7 at 1:18
add a comment |
$begingroup$
Clean, 54 bytes
import StdEnv
$n l=[l%(i,i+n-1)\i<-[0,n..length l-1]]
Try it online!
$endgroup$
add a comment |
$begingroup$
Clean, 54 bytes
import StdEnv
$n l=[l%(i,i+n-1)\i<-[0,n..length l-1]]
Try it online!
$endgroup$
add a comment |
$begingroup$
Clean, 54 bytes
import StdEnv
$n l=[l%(i,i+n-1)\i<-[0,n..length l-1]]
Try it online!
$endgroup$
Clean, 54 bytes
import StdEnv
$n l=[l%(i,i+n-1)\i<-[0,n..length l-1]]
Try it online!
answered Mar 6 at 11:51
ΟurousΟurous
7,41611136
7,41611136
add a comment |
add a comment |
$begingroup$
Python 2, 39 bytes
i,j=input()
while j:print j[:i];j=j[i:]
Try it online!
Assumes that 1 chunk per line is acceptable output.
$endgroup$
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
add a comment |
$begingroup$
Python 2, 39 bytes
i,j=input()
while j:print j[:i];j=j[i:]
Try it online!
Assumes that 1 chunk per line is acceptable output.
$endgroup$
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
add a comment |
$begingroup$
Python 2, 39 bytes
i,j=input()
while j:print j[:i];j=j[i:]
Try it online!
Assumes that 1 chunk per line is acceptable output.
$endgroup$
Python 2, 39 bytes
i,j=input()
while j:print j[:i];j=j[i:]
Try it online!
Assumes that 1 chunk per line is acceptable output.
answered Mar 6 at 14:15
ElPedroElPedro
3,5531023
3,5531023
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
add a comment |
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
4
4
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
36 bytes as a recursive lambda function
$endgroup$
– ovs
Mar 6 at 14:45
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
$begingroup$
@ovs - Very nice and also different enough for you to post as your own answer if you wish.
$endgroup$
– ElPedro
Mar 6 at 14:55
add a comment |
$begingroup$
Brainfuck, 71 bytes
,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]
Dunno if this counts or not... input format:
<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
1234567890123492034
n is 32 since the ASCII value of space is 32
Takes the input and puts in a space every time n characters pass
Explanation (no commas because that would break the program):
, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again
$endgroup$
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use,more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.
$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I haven n n A spaceas my cell setup, if you can think of a better way...
$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
CouldA space n n n ...work (orspace A n n n...)?
$endgroup$
– MilkyWay90
Mar 8 at 2:50
|
show 2 more comments
$begingroup$
Brainfuck, 71 bytes
,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]
Dunno if this counts or not... input format:
<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
1234567890123492034
n is 32 since the ASCII value of space is 32
Takes the input and puts in a space every time n characters pass
Explanation (no commas because that would break the program):
, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again
$endgroup$
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use,more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.
$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I haven n n A spaceas my cell setup, if you can think of a better way...
$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
CouldA space n n n ...work (orspace A n n n...)?
$endgroup$
– MilkyWay90
Mar 8 at 2:50
|
show 2 more comments
$begingroup$
Brainfuck, 71 bytes
,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]
Dunno if this counts or not... input format:
<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
1234567890123492034
n is 32 since the ASCII value of space is 32
Takes the input and puts in a space every time n characters pass
Explanation (no commas because that would break the program):
, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again
$endgroup$
Brainfuck, 71 bytes
,[>+>+<<-]>>>,[<[>.,<-]>>>++++[<++++++++>-]<.[-]<<<[<+>>+<-]<[->+<]>>>]
Dunno if this counts or not... input format:
<character whose ascii is n>AAAAAAAAAAAAA
For example, in the input:
1234567890123492034
n is 32 since the ASCII value of space is 32
Takes the input and puts in a space every time n characters pass
Explanation (no commas because that would break the program):
, take n
[>+>+<<-] copy into next two cells (destroys original)
>>>, take first of A into next cell
[ while that input exists
<[>.,<-] if n is nonzero output take next of A subtract one from n
>>>++++[<++++++++>-]<.[-]< n is zero so put a space
<<[<+>>+<-] copy the old n into surrounding cells
<[->+<] move from first cell to second
>>>] take input, do again
edited Mar 7 at 1:45
answered Mar 7 at 1:23
vityavvvityavv
659214
659214
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use,more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.
$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I haven n n A spaceas my cell setup, if you can think of a better way...
$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
CouldA space n n n ...work (orspace A n n n...)?
$endgroup$
– MilkyWay90
Mar 8 at 2:50
|
show 2 more comments
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use,more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.
$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I haven n n A spaceas my cell setup, if you can think of a better way...
$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
CouldA space n n n ...work (orspace A n n n...)?
$endgroup$
– MilkyWay90
Mar 8 at 2:50
2
2
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
Remove the spaces for 71 characters
$endgroup$
– MilkyWay90
Mar 7 at 1:31
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
lol, I thought I removed all of them but I didn't notice those, thanks!
$endgroup$
– vityavv
Mar 7 at 1:45
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use
, more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
Try reorganizing the cells such that the cells you use more are more accessible (for example, if the input cell (the one where you use
, more) is used more it could be put an a cell which is easier to access than if it was placed in other cells) or use a bruteforcer. I am not skilled in golfing in BF so these suggestions may not be helpful.$endgroup$
– MilkyWay90
Mar 7 at 3:06
$begingroup$
So far I have
n n n A space as my cell setup, if you can think of a better way...$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
So far I have
n n n A space as my cell setup, if you can think of a better way...$endgroup$
– vityavv
Mar 8 at 1:01
$begingroup$
Could
A space n n n ... work (or space A n n n...)?$endgroup$
– MilkyWay90
Mar 8 at 2:50
$begingroup$
Could
A space n n n ... work (or space A n n n...)?$endgroup$
– MilkyWay90
Mar 8 at 2:50
|
show 2 more comments
$begingroup$
Python 3, 46 chars
lambda A,n:[A[:n],*(f(A[n:],n)if A[n:]else)]
-1 thanks to @Collin Phillips.
Try it online!
$endgroup$
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
add a comment |
$begingroup$
Python 3, 46 chars
lambda A,n:[A[:n],*(f(A[n:],n)if A[n:]else)]
-1 thanks to @Collin Phillips.
Try it online!
$endgroup$
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
add a comment |
$begingroup$
Python 3, 46 chars
lambda A,n:[A[:n],*(f(A[n:],n)if A[n:]else)]
-1 thanks to @Collin Phillips.
Try it online!
$endgroup$
Python 3, 46 chars
lambda A,n:[A[:n],*(f(A[n:],n)if A[n:]else)]
-1 thanks to @Collin Phillips.
Try it online!
edited Mar 7 at 8:44
answered Mar 6 at 12:00
Henry THenry T
1816
1816
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
add a comment |
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
1
1
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
$begingroup$
I was able to get it to 46 characters with recursion
$endgroup$
– Collin Phillips
Mar 6 at 21:57
add a comment |
$begingroup$
CJam, 3 bytes
{/}
This is an anonymous block that takes an array of numbers and a number from the stack, and replaces them by an array of arrays.
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 3 bytes
{/}
This is an anonymous block that takes an array of numbers and a number from the stack, and replaces them by an array of arrays.
Try it online!
$endgroup$
add a comment |
$begingroup$
CJam, 3 bytes
{/}
This is an anonymous block that takes an array of numbers and a number from the stack, and replaces them by an array of arrays.
Try it online!
$endgroup$
CJam, 3 bytes
{/}
This is an anonymous block that takes an array of numbers and a number from the stack, and replaces them by an array of arrays.
Try it online!
answered Mar 6 at 11:49
Luis MendoLuis Mendo
75.3k889292
75.3k889292
add a comment |
add a comment |
$begingroup$
Brachylog, 2 bytes
ġ₎
Try it online!
$endgroup$
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
add a comment |
$begingroup$
Brachylog, 2 bytes
ġ₎
Try it online!
$endgroup$
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
add a comment |
$begingroup$
Brachylog, 2 bytes
ġ₎
Try it online!
$endgroup$
Brachylog, 2 bytes
ġ₎
Try it online!
answered Mar 6 at 12:09
FatalizeFatalize
28k449136
28k449136
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
add a comment |
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
$begingroup$
I read the statement, This came to mind immediately =). #builtinsFTW
$endgroup$
– Kroppeb
Mar 9 at 17:13
add a comment |
$begingroup$
Elixir, 16 bytes
Enum.chunk_every
Try it online!
$endgroup$
add a comment |
$begingroup$
Elixir, 16 bytes
Enum.chunk_every
Try it online!
$endgroup$
add a comment |
$begingroup$
Elixir, 16 bytes
Enum.chunk_every
Try it online!
$endgroup$
Elixir, 16 bytes
Enum.chunk_every
Try it online!
answered Mar 6 at 13:10
Kirill L.Kirill L.
6,0981527
6,0981527
add a comment |
add a comment |
$begingroup$
Charcoal, 1 byte
⪪
Try it online! Charcoal's default I/O makes it difficult to demonstrate using anything except strings. If you want a full program that takes numeric lists and outputs formatted lists then this can be done as follows:
E⪪AN⪫ι,
Try it online! Link is to verbose version of code. Explanation:
A Input array
⪪ Split into chunks of
N Input number
E Map over chunks
ι Current chunk
⪫ Joined with
, Literal `,`
Implicitly print each chunk on its own line
$endgroup$
add a comment |
$begingroup$
Charcoal, 1 byte
⪪
Try it online! Charcoal's default I/O makes it difficult to demonstrate using anything except strings. If you want a full program that takes numeric lists and outputs formatted lists then this can be done as follows:
E⪪AN⪫ι,
Try it online! Link is to verbose version of code. Explanation:
A Input array
⪪ Split into chunks of
N Input number
E Map over chunks
ι Current chunk
⪫ Joined with
, Literal `,`
Implicitly print each chunk on its own line
$endgroup$
add a comment |
$begingroup$
Charcoal, 1 byte
⪪
Try it online! Charcoal's default I/O makes it difficult to demonstrate using anything except strings. If you want a full program that takes numeric lists and outputs formatted lists then this can be done as follows:
E⪪AN⪫ι,
Try it online! Link is to verbose version of code. Explanation:
A Input array
⪪ Split into chunks of
N Input number
E Map over chunks
ι Current chunk
⪫ Joined with
, Literal `,`
Implicitly print each chunk on its own line
$endgroup$
Charcoal, 1 byte
⪪
Try it online! Charcoal's default I/O makes it difficult to demonstrate using anything except strings. If you want a full program that takes numeric lists and outputs formatted lists then this can be done as follows:
E⪪AN⪫ι,
Try it online! Link is to verbose version of code. Explanation:
A Input array
⪪ Split into chunks of
N Input number
E Map over chunks
ι Current chunk
⪫ Joined with
, Literal `,`
Implicitly print each chunk on its own line
answered Mar 6 at 13:22
NeilNeil
82.6k745179
82.6k745179
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 78 77 43 bytes
a=>b=>{int i=0;return a.GroupBy(_=>i++/b);}
Try it online!
I think we should be able to just write int i; because 0 is the default of int. I let it to avoid the error: error CS0165: Use of unassigned local variable 'i'.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 78 77 43 bytes
a=>b=>{int i=0;return a.GroupBy(_=>i++/b);}
Try it online!
I think we should be able to just write int i; because 0 is the default of int. I let it to avoid the error: error CS0165: Use of unassigned local variable 'i'.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 78 77 43 bytes
a=>b=>{int i=0;return a.GroupBy(_=>i++/b);}
Try it online!
I think we should be able to just write int i; because 0 is the default of int. I let it to avoid the error: error CS0165: Use of unassigned local variable 'i'.
$endgroup$
C# (Visual C# Interactive Compiler), 78 77 43 bytes
a=>b=>{int i=0;return a.GroupBy(_=>i++/b);}
Try it online!
I think we should be able to just write int i; because 0 is the default of int. I let it to avoid the error: error CS0165: Use of unassigned local variable 'i'.
edited Mar 6 at 14:41
answered Mar 6 at 14:33
aloisdgaloisdg
1,5721224
1,5721224
add a comment |
add a comment |
$begingroup$
F# (.NET Core), 15 bytes
Seq.chunkBySize
Try it online!
Well F# has a builtin...
$endgroup$
add a comment |
$begingroup$
F# (.NET Core), 15 bytes
Seq.chunkBySize
Try it online!
Well F# has a builtin...
$endgroup$
add a comment |
$begingroup$
F# (.NET Core), 15 bytes
Seq.chunkBySize
Try it online!
Well F# has a builtin...
$endgroup$
F# (.NET Core), 15 bytes
Seq.chunkBySize
Try it online!
Well F# has a builtin...
answered Mar 6 at 14:50
aloisdgaloisdg
1,5721224
1,5721224
add a comment |
add a comment |
$begingroup$
J, 4 bytes
<~-
Try it online!
Takes the array as left arg and chunk size as right arg.
Uses a dyadic hook and the infix adverb with a negative argument, which does what we want by definition.
Note: The return type must be boxed because J only allows tables of equal sized items.
$endgroup$
add a comment |
$begingroup$
J, 4 bytes
<~-
Try it online!
Takes the array as left arg and chunk size as right arg.
Uses a dyadic hook and the infix adverb with a negative argument, which does what we want by definition.
Note: The return type must be boxed because J only allows tables of equal sized items.
$endgroup$
add a comment |
$begingroup$
J, 4 bytes
<~-
Try it online!
Takes the array as left arg and chunk size as right arg.
Uses a dyadic hook and the infix adverb with a negative argument, which does what we want by definition.
Note: The return type must be boxed because J only allows tables of equal sized items.
$endgroup$
J, 4 bytes
<~-
Try it online!
Takes the array as left arg and chunk size as right arg.
Uses a dyadic hook and the infix adverb with a negative argument, which does what we want by definition.
Note: The return type must be boxed because J only allows tables of equal sized items.
answered Mar 6 at 16:06
JonahJonah
2,6611017
2,6611017
add a comment |
add a comment |
$begingroup$
Japt, 2 bytes
òV
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 2 bytes
òV
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt, 2 bytes
òV
Try it online!
$endgroup$
Japt, 2 bytes
òV
Try it online!
answered Mar 6 at 11:45
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,70821671
5,70821671
add a comment |
add a comment |
$begingroup$
PHP, 45 bytes
function f($a,$b){return array_chunk($a,$b);}
Try it online!
$endgroup$
3
$begingroup$
Would justarray_chunkbe a valid answer?
$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
add a comment |
$begingroup$
PHP, 45 bytes
function f($a,$b){return array_chunk($a,$b);}
Try it online!
$endgroup$
3
$begingroup$
Would justarray_chunkbe a valid answer?
$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
add a comment |
$begingroup$
PHP, 45 bytes
function f($a,$b){return array_chunk($a,$b);}
Try it online!
$endgroup$
PHP, 45 bytes
function f($a,$b){return array_chunk($a,$b);}
Try it online!
answered Mar 6 at 12:04
Luis felipe De jesus MunozLuis felipe De jesus Munoz
5,70821671
5,70821671
3
$begingroup$
Would justarray_chunkbe a valid answer?
$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
add a comment |
3
$begingroup$
Would justarray_chunkbe a valid answer?
$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
3
3
$begingroup$
Would just
array_chunk be a valid answer?$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
Would just
array_chunk be a valid answer?$endgroup$
– Arnauld
Mar 6 at 12:19
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
@Arnauld I dont know. Never golfed in php before although I use it at work.
$endgroup$
– Luis felipe De jesus Munoz
Mar 6 at 12:21
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
I'm not 100% sure either, but we can abuse the implicit conversion of undeclared variables to a string and do something like that.
$endgroup$
– Arnauld
Mar 6 at 12:30
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
$begingroup$
(erratum: I meant undefined constants)
$endgroup$
– Arnauld
Mar 6 at 12:38
add a comment |
$begingroup$
Java 10, 106 80 bytes
L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}
Prints the chunks without delimiter.
Try it online.
106 bytes:
L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}
Actually returns a list of lists.
Try it online.
Explanation:
L->n->{ // Method with List and integer parameters and List return-type
var r=new java.util.Stack();// Create an empty List
for(int l=L.size(), // Determine the size of the input-List
i=0;i<l;) // Loop `i` in the range [0, size):
r.add( // Add to the result-List:
L.subList(i, // A sublist of the input-list in the range from `i`
Math.min(i+=n,l))); // to the minimum of: `i` + input-integer or the size
// (and increase `i` by the input-integer at the same)
return r;} // Return the List of Lists of integers as result
$endgroup$
add a comment |
$begingroup$
Java 10, 106 80 bytes
L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}
Prints the chunks without delimiter.
Try it online.
106 bytes:
L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}
Actually returns a list of lists.
Try it online.
Explanation:
L->n->{ // Method with List and integer parameters and List return-type
var r=new java.util.Stack();// Create an empty List
for(int l=L.size(), // Determine the size of the input-List
i=0;i<l;) // Loop `i` in the range [0, size):
r.add( // Add to the result-List:
L.subList(i, // A sublist of the input-list in the range from `i`
Math.min(i+=n,l))); // to the minimum of: `i` + input-integer or the size
// (and increase `i` by the input-integer at the same)
return r;} // Return the List of Lists of integers as result
$endgroup$
add a comment |
$begingroup$
Java 10, 106 80 bytes
L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}
Prints the chunks without delimiter.
Try it online.
106 bytes:
L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}
Actually returns a list of lists.
Try it online.
Explanation:
L->n->{ // Method with List and integer parameters and List return-type
var r=new java.util.Stack();// Create an empty List
for(int l=L.size(), // Determine the size of the input-List
i=0;i<l;) // Loop `i` in the range [0, size):
r.add( // Add to the result-List:
L.subList(i, // A sublist of the input-list in the range from `i`
Math.min(i+=n,l))); // to the minimum of: `i` + input-integer or the size
// (and increase `i` by the input-integer at the same)
return r;} // Return the List of Lists of integers as result
$endgroup$
Java 10, 106 80 bytes
L->n->{for(int l=L.size(),i=0;i<l;)System.out.print(L.subList(i,(i+=n)<l?i:l));}
Prints the chunks without delimiter.
Try it online.
106 bytes:
L->n->{var r=new java.util.Stack();for(int l=L.size(),i=0;i<l;)r.add(L.subList(i,(i+=n)<l?i:l));return r;}
Actually returns a list of lists.
Try it online.
Explanation:
L->n->{ // Method with List and integer parameters and List return-type
var r=new java.util.Stack();// Create an empty List
for(int l=L.size(), // Determine the size of the input-List
i=0;i<l;) // Loop `i` in the range [0, size):
r.add( // Add to the result-List:
L.subList(i, // A sublist of the input-list in the range from `i`
Math.min(i+=n,l))); // to the minimum of: `i` + input-integer or the size
// (and increase `i` by the input-integer at the same)
return r;} // Return the List of Lists of integers as result
edited Mar 6 at 12:08
answered Mar 6 at 11:54
Kevin CruijssenKevin Cruijssen
42.6k571217
42.6k571217
add a comment |
add a comment |
$begingroup$
K (oK), 10 bytes
{(0N,x)#y}
Try it online!
$endgroup$
add a comment |
$begingroup$
K (oK), 10 bytes
{(0N,x)#y}
Try it online!
$endgroup$
add a comment |
$begingroup$
K (oK), 10 bytes
{(0N,x)#y}
Try it online!
$endgroup$
K (oK), 10 bytes
{(0N,x)#y}
Try it online!
answered Mar 6 at 12:13
Galen IvanovGalen Ivanov
7,41211034
7,41211034
add a comment |
add a comment |
$begingroup$
Ruby, 25 bytes
->n,a{[*a.each_slice(n)]}
Try it online!
If we can return enumerators instead of arrays, then it becomes simply:
Ruby, 21 bytes
->n,a{a.each_slice n}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n,a{[*a.each_slice(n)]}
Try it online!
If we can return enumerators instead of arrays, then it becomes simply:
Ruby, 21 bytes
->n,a{a.each_slice n}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 25 bytes
->n,a{[*a.each_slice(n)]}
Try it online!
If we can return enumerators instead of arrays, then it becomes simply:
Ruby, 21 bytes
->n,a{a.each_slice n}
Try it online!
$endgroup$
Ruby, 25 bytes
->n,a{[*a.each_slice(n)]}
Try it online!
If we can return enumerators instead of arrays, then it becomes simply:
Ruby, 21 bytes
->n,a{a.each_slice n}
Try it online!
edited Mar 6 at 12:55
answered Mar 6 at 12:49
Kirill L.Kirill L.
6,0981527
6,0981527
add a comment |
add a comment |
$begingroup$
PicoLisp, 75 74 bytes
(de f(n l)(if(>= n(length l))(list l)(cons(head n l)(f n(tail(- 0 n)l)))))
Try it online!
$endgroup$
add a comment |
$begingroup$
PicoLisp, 75 74 bytes
(de f(n l)(if(>= n(length l))(list l)(cons(head n l)(f n(tail(- 0 n)l)))))
Try it online!
$endgroup$
add a comment |
$begingroup$
PicoLisp, 75 74 bytes
(de f(n l)(if(>= n(length l))(list l)(cons(head n l)(f n(tail(- 0 n)l)))))
Try it online!
$endgroup$
PicoLisp, 75 74 bytes
(de f(n l)(if(>= n(length l))(list l)(cons(head n l)(f n(tail(- 0 n)l)))))
Try it online!
edited Mar 6 at 13:43
answered Mar 6 at 13:12
Galen IvanovGalen Ivanov
7,41211034
7,41211034
add a comment |
add a comment |
$begingroup$
Coconut, 8 bytes
groupsof
Try it online!
$endgroup$
add a comment |
$begingroup$
Coconut, 8 bytes
groupsof
Try it online!
$endgroup$
add a comment |
$begingroup$
Coconut, 8 bytes
groupsof
Try it online!
$endgroup$
Coconut, 8 bytes
groupsof
Try it online!
answered Mar 6 at 14:54
ovsovs
19.5k21161
19.5k21161
add a comment |
add a comment |
$begingroup$
V, 6 bytes
òÀf,r
Try it online!
Hexdump:
00000000: f2c0 662c 720a ..f,r.
Explanation:
ò " Until an error happens:
f " (f)ind the...
À " n'th...
, " ","
" (If there are less than n commas after the cursor, throw an error)
r " Replace the char under the cursor with...
<cr> " A newline
$endgroup$
add a comment |
$begingroup$
V, 6 bytes
òÀf,r
Try it online!
Hexdump:
00000000: f2c0 662c 720a ..f,r.
Explanation:
ò " Until an error happens:
f " (f)ind the...
À " n'th...
, " ","
" (If there are less than n commas after the cursor, throw an error)
r " Replace the char under the cursor with...
<cr> " A newline
$endgroup$
add a comment |
$begingroup$
V, 6 bytes
òÀf,r
Try it online!
Hexdump:
00000000: f2c0 662c 720a ..f,r.
Explanation:
ò " Until an error happens:
f " (f)ind the...
À " n'th...
, " ","
" (If there are less than n commas after the cursor, throw an error)
r " Replace the char under the cursor with...
<cr> " A newline
$endgroup$
V, 6 bytes
òÀf,r
Try it online!
Hexdump:
00000000: f2c0 662c 720a ..f,r.
Explanation:
ò " Until an error happens:
f " (f)ind the...
À " n'th...
, " ","
" (If there are less than n commas after the cursor, throw an error)
r " Replace the char under the cursor with...
<cr> " A newline
edited Mar 6 at 19:45
answered Mar 6 at 19:40
DJMcMayhem♦DJMcMayhem
41.1k12149315
41.1k12149315
add a comment |
add a comment |
$begingroup$
Clojure, 14 bytes
#(partition %)
builtins I guess
$endgroup$
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
add a comment |
$begingroup$
Clojure, 14 bytes
#(partition %)
builtins I guess
$endgroup$
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
add a comment |
$begingroup$
Clojure, 14 bytes
#(partition %)
builtins I guess
$endgroup$
Clojure, 14 bytes
#(partition %)
builtins I guess
answered Mar 6 at 20:51
nihilazonihilazo
513
513
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
add a comment |
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
$begingroup$
Hi, welcome. The function should take two arguments: the array to be partitioned and the length of the chunk. Also what happens if the last chunk isn't "full" when using partition?
$endgroup$
– NikoNyrh
Mar 7 at 17:05
add a comment |
$begingroup$
Haskell, 26 bytes
import Data.Lists
chunksOf
Here's a more interesting version, with just a few more bytes (thanks to nimi for five bytes in each solution):
Haskell, 31 bytes
n!=
n!x=take n x:n!drop n x
Try it online!
$endgroup$
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
$begingroup$
n!x=take n x:n!drop n x.Data.Listsprovides alsochunksOf.
$endgroup$
– nimi
Mar 6 at 23:00
add a comment |
$begingroup$
Haskell, 26 bytes
import Data.Lists
chunksOf
Here's a more interesting version, with just a few more bytes (thanks to nimi for five bytes in each solution):
Haskell, 31 bytes
n!=
n!x=take n x:n!drop n x
Try it online!
$endgroup$
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
$begingroup$
n!x=take n x:n!drop n x.Data.Listsprovides alsochunksOf.
$endgroup$
– nimi
Mar 6 at 23:00
add a comment |
$begingroup$
Haskell, 26 bytes
import Data.Lists
chunksOf
Here's a more interesting version, with just a few more bytes (thanks to nimi for five bytes in each solution):
Haskell, 31 bytes
n!=
n!x=take n x:n!drop n x
Try it online!
$endgroup$
Haskell, 26 bytes
import Data.Lists
chunksOf
Here's a more interesting version, with just a few more bytes (thanks to nimi for five bytes in each solution):
Haskell, 31 bytes
n!=
n!x=take n x:n!drop n x
Try it online!
edited Mar 6 at 23:06
answered Mar 6 at 22:26
dfeuerdfeuer
9061011
9061011
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
$begingroup$
n!x=take n x:n!drop n x.Data.Listsprovides alsochunksOf.
$endgroup$
– nimi
Mar 6 at 23:00
add a comment |
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
$begingroup$
n!x=take n x:n!drop n x.Data.Listsprovides alsochunksOf.
$endgroup$
– nimi
Mar 6 at 23:00
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
$begingroup$
I think you can
$endgroup$
– aloisdg
Mar 6 at 22:27
1
1
$begingroup$
n!x=take n x:n!drop n x. Data.Lists provides also chunksOf.$endgroup$
– nimi
Mar 6 at 23:00
$begingroup$
n!x=take n x:n!drop n x. Data.Lists provides also chunksOf.$endgroup$
– nimi
Mar 6 at 23:00
add a comment |
$begingroup$
PowerShell, 67 65 bytes
-2 bytes thanks AdmBorkBork
param($n,$a)$a|%{$b+=,$_
if($b.Count-ge$n){,$b;rv b}}
if($b){,$b}
Try it online!
$endgroup$
2
$begingroup$
You should be able torv b(alias forRemove-Variable) instead of$b=@()to save two bytes.
$endgroup$
– AdmBorkBork
Mar 6 at 20:45
add a comment |
$begingroup$
PowerShell, 67 65 bytes
-2 bytes thanks AdmBorkBork
param($n,$a)$a|%{$b+=,$_
if($b.Count-ge$n){,$b;rv b}}
if($b){,$b}
Try it online!
$endgroup$
2
$begingroup$
You should be able torv b(alias forRemove-Variable) instead of$b=@()to save two bytes.
$endgroup$
– AdmBorkBork
Mar 6 at 20:45
add a comment |
$begingroup$
PowerShell, 67 65 bytes
-2 bytes thanks AdmBorkBork
param($n,$a)$a|%{$b+=,$_
if($b.Count-ge$n){,$b;rv b}}
if($b){,$b}
Try it online!
$endgroup$
PowerShell, 67 65 bytes
-2 bytes thanks AdmBorkBork
param($n,$a)$a|%{$b+=,$_
if($b.Count-ge$n){,$b;rv b}}
if($b){,$b}
Try it online!
edited Mar 7 at 3:39
answered Mar 6 at 17:15
mazzymazzy
2,9851318
2,9851318
2
$begingroup$
You should be able torv b(alias forRemove-Variable) instead of$b=@()to save two bytes.
$endgroup$
– AdmBorkBork
Mar 6 at 20:45
add a comment |
2
$begingroup$
You should be able torv b(alias forRemove-Variable) instead of$b=@()to save two bytes.
$endgroup$
– AdmBorkBork
Mar 6 at 20:45
2
2
$begingroup$
You should be able to
rv b (alias for Remove-Variable) instead of $b=@() to save two bytes.$endgroup$
– AdmBorkBork
Mar 6 at 20:45
$begingroup$
You should be able to
rv b (alias for Remove-Variable) instead of $b=@() to save two bytes.$endgroup$
– AdmBorkBork
Mar 6 at 20:45
add a comment |
$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$
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
add a comment |
$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$
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
add a comment |
$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$
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.
edited Mar 13 at 12:40
answered Mar 6 at 12:08
VenVen
2,55511223
2,55511223
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
add a comment |
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
1
1
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
This night give a better output as to showing that single element arrays are still actually arrays.
$endgroup$
– Nick Kennedy
Mar 6 at 13:33
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
Er, is the downvote because I didn’t add @Nick Kennedy’s link?
$endgroup$
– Ven
Mar 7 at 7:48
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
$begingroup$
certainly not from me
$endgroup$
– Nick Kennedy
Mar 7 at 7:49
add a comment |
1 2
next
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180970%2fcreate-chunks-from-an-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
If
nis greater than the length ofAwe need to returnA‽ Are you sure you don't mean[A]?$endgroup$
– Adám
Mar 6 at 13:24
9
$begingroup$
@chaugiang I still think a too large
nshould return[A], e.g[[1,2,3]]. What ifnis exactly the length ofA?$endgroup$
– Adám
Mar 6 at 13:42
4
$begingroup$
@chaugiang Adam is correct imo. The return value should be consistent.
$endgroup$
– Jonah
Mar 6 at 16:02
1
$begingroup$
@chaugiang Can n ever equal 1?
$endgroup$
– DJMcMayhem♦
Mar 6 at 19:41
3
$begingroup$
In a strongly typed language, it's simply impossible to return
Arather than[A], which would exclude an awful lot of languages.$endgroup$
– dfeuer
Mar 6 at 22:48