I have the total number of elements in the range N
and a number of chunks nb
I want to divide N
into nb
best possible equal ranges, with just the start number and the end number. So for example, N=24
and nb=5
should output:
0,5 5,10 10,15 15,20 20,24
While N=28
and nb=5
should output:
0,5 5,10 10,16 16,22 22,28 (the rest of `N/nb` division is equally distributed on the 3 last subranges)
Based on one comment, I have this method:
def partition(lst, n):
division = len(lst) / n
return [lst[round(division * i):round(division * (i + 1))] for i in range(n)]
def ranges(N, nb):
return ["{},{}".format(r.start, r.stop) for r in partition(range(N), nb)]
>>> ranges(28, 5)
['0,6', '6,11', '11,17', '17,22', '22,28']
Is there a better way to do this?