Skip to main content
added 790 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 6 bytes

vi3$es 

Try it online! Or verify all test cases.

Explanation

Consider input 4, [0,3,7,2,5,1] as an example.

v % Vertically concatenate stack contents. Gives the empty array, [] % STACK: [] i % Input b % STACK: [], 4 % Implicitly input L at the bottom of the stack % STACK: [0,3,7,2,5,1], [], 4 3$e % 3-input reshape. This reshapes L with [] rows and b columns, in % column-major order (down, then across). [] here means that the % number of rows is chosen as needed to give b columns. Padding % with trailing zeros is applied if needed % STACK: [0 7 5 0; 3 2 1 0] s % Sum of each column % STACK: [3 9 6 0] % Implicitly display 

MATL, 6 bytes

vi3$es 

Try it online! Or verify all test cases.

Explanation

Consider input 4, [0,3,7,2,5,1] as an example.

v % Vertically concatenate stack contents. Gives the empty array, [] % STACK: [] i % Input b % STACK: [], 4 % Implicitly input L at the bottom of the stack % STACK: [0,3,7,2,5,1], [], 4 3$e % 3-input reshape. This reshapes L with [] rows and b columns, in % column-major order (down, then across). [] here means that the % number of rows is chosen as needed to give b columns. Padding % with trailing zeros is applied if needed % STACK: [0 7 5 0; 3 2 1 0] s % Sum of each column % STACK: [3 9 6 0] % Implicitly display 
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382