The challenge - given a numeric list L and an integer N as inputs, write a function that:
- finds the bucket sizes for L such that it is split into N whole buckets of equal or near-equal size, and
- returns for each element in L the minimum of that element's bucket.
Example -
L=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
N=5
Compute the bucket sizes with the following:
\$\left \lfloor \frac{| L |}{N} \right \rfloor + 1\$ elements go into \$|L| \bmod N\$ buckets, and
\$\left \lfloor \frac{| L |}{N} \right \rfloor \$ go into the rest,
where |L| is the length of L. For the above, we get
floor(12/5)+1
into 12 mod 5
buckets, and floor(12/5)
in the rest:
[[0, 1, 2], [3, 4, 5], [6, 7], [8, 9], [10, 11]]
Finally, we output a list where each element is the minimum of its bucket:
[0, 0, 0, 3, 3, 3, 6, 6, 8, 8, 10, 10]
Some test cases:
In -> Out
[1, 2, 3, 4], 1 -> [1, 1, 1, 1]
[1, 2, 3, 4], 2 -> [1, 1, 3, 3]
[0, 2, 4, 6, 8], 3 -> [0, 0, 4, 4, 8]
[9, 3, 0, 1, 5, 7, 4, 6, 8, 10, 12, 11, 2, 13], 5 -> [0, 0, 0, 1, 1, 1, 4, 4, 4, 10, 10, 10, 2, 2]
[3, 0, -2, 0, -1], 2 -> [-2, -2, -2, -1, -1]
This is a code golf challenge. Shortest answer in bytes wins. Standard loopholes apply and such.
Edit:
you may assume L is not empty
you do not have to handle the case where N>|L|
your answer should return a flat list