Binning: Which Bucket Does Each Value Land In?

Feature EngineeringEasy+10 XP+10

Binning: Which Bucket Does Each Value Land In?

Binning turns a continuous feature into a handful of ordered buckets: ages become age brackets, incomes become income bands. You throw away precision deliberately, in exchange for a feature a tree can split on cleanly and a human can read.

Task: write bin_values(values, edges) returning each value's bin index.

edges is a sorted list of cut points, and n edges make n + 1 bins:

BinRange
0up to and including edges[0]
1above edges[0], up to and including edges[1]
……
nabove edges[-1]

So every bin is open on the left and closed on the right, and a value landing exactly on an edge belongs to the lower bin.

  • The first and last bins are unbounded, so no value can ever fall outside the scheme.
  • edges may be empty, in which case there's one bin and everything is 0.
  • Values and edges may be negative.
  • Return plain integers.

The short way to compute it: a value's bin index is simply how many edges it is strictly greater than. That single count handles the inclusive side, both unbounded ends, and the empty-edges case, with no branching at all.

Which side the edges close on is arbitrary but must be decided and written down — it's the difference between a 30-year-old landing in the 18–30 bracket or the 30–50 one, and "whatever the code does" is how two pipelines end up disagreeing about the same person.

Examples

values = [1, 5, 10, 15]
edges = [5, 10]
returns [0, 0, 1, 2]
values = [-5, 0, 5]
edges = [-1, 1]
returns [0, 1, 2]

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