Min-Max Scaling: Each Reading Scaled Once, on Arrival

Data ProcessingMedium+15 XP+15

Min-Max Scaling: Each Reading Scaled Once, on Arrival

Ordinary min-max scaling needs the whole column up front: you find the smallest and largest values, then squeeze everything onto [0, 1]. A live system doesn't have that luxury — readings arrive one at a time and each has to be scaled the moment it lands, using only what has been seen so far.

Task: write streaming_minmax(stream) returning one scaled value per incoming value, each rounded to 4 decimal places.

For each value, in order:

  1. Update the running minimum and maximum to include the value you just received.
  2. Scale it with (value - min_so_far) / (max_so_far - min_so_far).
  • The current value counts toward the min and max before you scale it. That's why the very first value always scales to 0.0 — it is simultaneously the smallest and largest thing seen.
  • When everything so far is identical, max - min is zero. Return 0.0 for that value rather than dividing by zero.
  • Scale each value only once, as it arrives. You never go back and rescale earlier outputs, even though a later extreme would have changed them.

That last rule is the honest part and the uncomfortable part: the same reading can map to different numbers depending on when it showed up. Offline scaling can't tell you that; a live pipeline has to live with it.

Examples

stream = [5, 10, 2, 8]
returns [0.0, 1.0, 0.0, 0.75]
stream = [3, 3, 3]
returns [0.0, 0.0, 0.0]

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