Your challenge today is to take an array, split it into chunks, and add those chunks.

Here's how this works: Your program or function will be given an array of integers a and a chunk size L. The array should be split into arrays of size L, if the array length is not divisible by L then the array should have 0's appended to it so that it is evenly divisible. Once the array is chunked, all chunks are to be added together element-wise. The resulting array is then output.

You can assume L is greater than 0, and that a is nonempty. You cannot make the assumption that a's contents are positive.

Here's an example:

[1,2,3,4,5,6,7,8], 3 => [1,2,3]+[4,5,6]+[7,8,0] =>  [1+4+7,2+5+8,3+6+0] => [12,15,9]


Test cases:

Array                           Length   Output
[1]                             1        [1]
[1]                             3        [1,0,0]
[0]                             3        [0,0,0]
[1,2]                           3        [1,2,0]
[1,2]                           1        [3]
[-1,1]                          2        [-1,1]
[-7,4,-12,1,5,-3,12,0,14,-2]    4        [12,-1,0,1]
[1,2,3,4,5,6,7,8,9]             3        [12,15,18]


This is , fewest bytes wins!

• Isn't it a dupe? – sergiol Sep 18 '17 at 23:32
• @sergiol if you can find a question that this is a duplicate of, I will delete this post myself. However, as far as I can tell it is not a dupe. – Pavel Sep 18 '17 at 23:54

[_nwise($n)]|transpose|map(add)  Sample Run $ jq -Mc --argjson n 3 '[_nwise($n)]|transpose|map(add)' <<< "[1,2,3,4,5,6,7,8]" [12,15,9]  Takes advantage of builtins: _nwise, transpose, add and map Javascript, 5957 56 bytes f=(a,l)=>(r=Array(l).fill(0),a.forEach((c,i)=>r[i%l]+=c),r)  usage f([1,2,3,4,5,6,7,8], 3); // returns [12, 15, 9]  edit 1 According to @Shaggy in the comments, the f= part is not needed if the function is not recursive. So: (a,l)=>(r=Array(l).fill(0),a.forEach((c,i)=>r[i%l]+=c),r)  with usage off course of: ((a,l)=>(r=Array(l).fill(0),a.forEach((c,i)=>r[i%l]+=c),r))([1,2,3,4,5,6,7,8], 3) // returns [12, 15, 9]  edit 2 Golfing down 1 more character, as the extra parenthesis are no longer needed then. (a,l,r=Array(l).fill(0))=>a.forEach((c,i)=>r[i%l]+=c)||r  with usage off course of: ((a,l,r=Array(l).fill(0))=>a.forEach((c,i)=>r[i%l]+=c)||r)([1,2,3,4,5,6,7,8], 3) // returns [12, 15, 9]  • You were beaten to it. – Shaggy Sep 19 '17 at 11:23 • @Shaggy I don't think that solution is correct as per the rules. Also he is leaving out the f= part that should count for more characters. – nl-x Sep 19 '17 at 11:30 • How so? It's identical to yours, just more golfed. Functions only need to be assigned to a variable if they're recursive. – Shaggy Sep 19 '17 at 11:31 • The assignment is a program or a function that returns the resulting array. This function returns another function that needs to be executed again. Also the f= part is needed to call the function. If it is not needed, I can also golf down 2 characters. – nl-x Sep 19 '17 at 11:44 • Currying, like anonymous functions, is also perfectly valid. See here. – Shaggy Sep 19 '17 at 11:49 Common Lisp, 90 bytes (lambda(a l)(dotimes(i(-(length a)l)(subseq a 0 l))(incf(elt a(mod i l))(elt a (+ l i)))))  Try it online! POSIX shell, 45 bytes This requires the utilities jot and rs to be installed, as well as the more common sed and bc. The chunk size is passed as the first argument, and the array as space-separated values on standard input (jot -b0$1;cat)|rs -tjg1 -C+ $1|sed s/.//|bc  The jot command generates a row full of zeros (in case the chunk size is smaller than the input); cat appends the input, then rs transforms that into the specified number of rows. Each line now begins with +, and bc won't accept that, so remove it with sed before performing the summation of each line. Ruby, 43 bytes ->a,n{b=[w=0]*n;a.map{|r|b[w%n]+=r;w+=1};b}  I wanted to outgolf at least python, but I couldn't, so here is the best I could do. How it works: • create an array of size n filled with zeroes • iterate on the input array, and calculate sums, repeat wrapping around the output array Try it online! Scala, 75 bytes a=>n=>(a++Seq.fill(n-a.size/n)(0)).grouped(n).toSeq.transpose.map(_.sum)  To use this, you have to add a type: val f:(Seq[Int]=>Int=>Seq[Int])= a=>n=>(a++Seq.fill(n-a.size/n)(0)).grouped(n).toSeq.transpose.map(_.sum) f(Seq(1,2,3,4,5,6,7,8))(3)  Pari/GP, 33 bytes a->l->Vecrev(Polrev(a)%(x^l-1),l)  Converts a to a polynomial, mod x^l-1, and then converts it back to a list. Try it online! Julia 0.6, 49 bytes V\n=sum(reshape([V;zeros(n-endof(V)%n,1)],n,:),2)  Try it online! Add++, 25 bytes D,f,@@*,d0b]*$@\$+TbUpBcBs

First, we define our function $f(a, x)$. It takes two arguments (@@) and returns the entire stack (*), rather than the top element. Here, $a$ denotes the array of elements, such as $a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]$, and $x$ represents slice length, $x := 4$ for example. The stack is $[a, x]$ at the start of execution. We then duplicate $x$, and repeat $[0]$ $x$ times. We call this array $z$, rearrange the stack so it looks like $[x, a, z]$ and concatenate $a$ with $z$. This appends $x$ zeroes to the end of $a$.
Next, we take slices of length $x$ from $a ^ \frown z$. For the example inputs above, this would yield $[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 0, 0], [0, 0]]$. For every possible inputs, this yields an array where the final element always contains only superfluous zeroes, so we splat the elements of the array to the stack and remove the last element. Finally, we zip the arrays together and take the sum of each.