I am trying to convert an integer (num
) to bins as per given interval.
the bin size (intervals) are [1, 200), [200, 400), [400, 800), [800, 1200), [1200, num]
I'am doing it in a crude way ...
def create_bins(num):
"""Create Bins as per given intervals."""
s1, s2, s3, s4 = 199, 200, 400, 400
if num > 1200:
res = [s1, s2, s3, s4, num - (s1 + s2 + s3 + s4)]
elif num < 1200 and num >= 800:
res = [s1, s2, s3, num - (s1 + s2 + s3)]
elif num < 800 and num >= 400:
res = [s1, s2, num - (s1 + s2)]
elif num < 400 and num >= 200:
res = [s1, num - s1]
else:
res = [num]
return res
this function for create_bins(1041)
returns [199, 200, 400, 242]
which is correct. However, I'am sure there are better ways to get it done...
Will appreciate if you can lead me towards better solutions for these kind of problem.