Question details showed in the picture Thanks for your help.
Write a function histogram(values, dividers) that takes as argument a sequence of values and a sequence of bin dividers, and returns the histogram as a sequence of a suitable type (say, an array) with the counts in each bin. The number of bins is the number of dividers + 1; the first bin has no lower limit and the last bin has no upper limit. As in (a), elements that are equal to one of the dividers are counted in the bin below.
For example, suppose the sequence of values is the numbers 1,..,10 and the bin dividers are array(2, 5, 7); the histogram should be array(2, 3, 2, 3).
Here is my code
def histogram(values, dividers):
count=0
for element in values:
index=0
i=0
count[i]=0
while index < len(dividers) - 2:
if element <= dividers[index]:
i=dividers[index]
count[i] += 1
index=len(dividers)
elif element > dividers[index] and element <= dividers[index+1]:
i=dividers[index]
count[i] += 1
index= len(dividers)
index += 1
return count[i]