Fix the stale reading filter for heart rate

Review the AI's codeEasy10 minMeta style

Your task

An AI wrote a React Native screen that shows heart rate from a Bluetooth sensor. It never removed its event listener, and it deferred every update with setTimeout(..., 0), so readings landed out of order and the number sometimes jumped backward. Your team pins down exactly what the screen should show for a stream of readings.

An assistant wrote apply_heart_rate_samples(sample_lines, unmount_after_index). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • sample_lines: the readings in the order they arrived. Each is timestamp:bpm, where the timestamp is when the sensor took the reading, in ms.
  • unmount_after_index: the index at which the screen unmounted and the listener was removed.
  • Return the numbers the screen shows, in order.

Rules the tests check:

  • Only readings at an index below unmount_after_index are delivered. Ignore the rest.
  • Split a reading at its first :. Skip it unless both parts are one or more digits from 0 to 9. A skipped reading changes nothing.
  • Show a reading only if its timestamp is greater than the timestamp of the last reading shown. The first valid reading is always shown.

Examples

apply_heart_rate_samples(sample_lines: list[str], unmount_after_index: int) -> list[int]
Example 1
Inputapply_heart_rate_samples(["1000:72", "2000:74", "3000:73"], 100)
Output[72, 74, 73]
Example 2
Inputapply_heart_rate_samples(["1000:72", "3000:80", "2000:60"], 100)
Output[72, 80]