All problems
Histogram Bin Counts
Problem
Bin data into n_bins equal-width bins starting at lo, each of width width: bin i covers [lo + i*width, lo + (i+1)*width). Values outside [lo, lo + width*n_bins] are ignored and the right edge of the last bin is included. Return the list of counts.
Hint
The bin index is floor((x - lo) / width); special-case the right edge.
Test cases
case 1
in: [[0,1,2,3,4],0,1,2]
expected: [1,2]
case 2
in: [[1,1.5,2.5,5],1,1,4]
expected: [2,1,0,1]
case 3
in: [[],0,5,2]
expected: [0,0]
case 4
in: [[10],0,5,2]
expected: [0,1]
case 5
in: [[0.5,1.5,2.5,3.5],0.5,1,3]
expected: [1,1,2]
Starter codePython
def histogram_counts(data, lo, width, n_bins):
# Your code here
pass5 test cases · run your code against them in the editor