Yield the Change Between Readings

Medium ⏱ 12 min 62% acceptance ★★★★★ 4.7
Write the generator deltas(readings) yielding the difference between each consecutive pair from ANY iterable — including one-shot iterators, so you cannot index or zip the input with itself via two passes. Track the previous value as you go.

Examples

Example 1
Input
list(deltas(iter([10, 13, 11])))
Output
[3, -2]
Explanation

Works on a pure iterator: 13-10 and 11-13.

Constraints

  • Single pass, no indexing.
  • Zero or one readings yield nothing.

Topics

Iterators & Generatorspairwise

Companies

TeslaBoschHoneywell

Hints

Hint 1

Prime prev with next(it, sentinel).

Hint 2

Yield current - prev, then advance prev.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.