Given a list of integers L
, and an integer N
, output L
splitted in N
sublists of equal lenghts.
Non-divisible lengths
If N
does not divide the length of L
, then it is not possible that all sublists have equal length.
In any case, the last sublist of the output is the one that adapts its length to contain the remainder of the list.
This means that all sublists of L
except for the last one should be of length length(L) // N
, where //
is floored division (e.g. 3//2 = 1
).
Some rules
L
can be empty.N >= 1
.You may use any built-in you want.
You may take the input through
STDIN
, as a function argument, or anything similar.You may print the output to
STDOUT
, return it from a function, or anything similar.You may chose any format for the lists and the integer as long as it is the most natural representation of lists and integers in your language.
Test cases
Input: [1,2,3,4], 2
Output: [[1,2],[3,4]]
Input: [-1,-2,3,4,-5], 2
Output: [[-1,-2],[3,4,-5]]
Input: [1,2,3,4], 1
Output: [[1,2,3,4]]
Input: [4,8,15,16,23,42], 5
Output: [[4],[8],[15],[16],[23,42]]
Input: [4,8,15,16,23,42], 7
Output: [[],[],[],[],[],[],[4,8,15,16,23,42]]
Input: [2,3,5,7,11,13,17,19,23], 3
Output: [[2,3,5],[7,11,13],[17,19,23]]
Input: [], 3
Output: [[],[],[]]
Input: [1,2,3,4,5,6,7,8], 3
Output: [[1,2],[3,4],[5,6,7,8]]
Scoring
This is code-golf, so the shortest answer in bytes wins.