Fill the Tubes

Data with PythonMedium+10 XP+10

Fill the Tubes

Before trusting a histogram, a data analyst wants to count the bars herself.

Task: write bin_counts(values, bins) that returns a list of bins whole numbers: how many values land in each bin, from the lowest bin to the highest. Use the same rule as ax.hist:

  • The bins split the range from the smallest value to the largest value into bins bins of equal width.
  • A value sitting exactly on the edge between two bins goes into the bin on its right.
  • The largest value goes into the last bin.

values always holds at least two different numbers, and bins is at least 1.

Examples

values = [0, 5, 20, 25, 40, 45, 60, 100]
bins = 5
returns [2, 2, 2, 1, 1]
values = [1, 2, 2, 3, 3, 3, 4, 4, 5]
bins = 2
returns [3, 6]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run