Sliding Windows over Any Iterable

easy
Unit 8 · Dataclasses0/3 to milestone

Implement windowed(iterable, n, step=1), a generator of tuples of n consecutive items, advancing step items each time. It must work on infinite and one-shot iterators, holding at most n items in memory (use collections.deque(maxlen=n)).

Don't yield a final short window. If the input is shorter than n, yield nothing.

Then implement pairwise_diffs(iterable): a generator of differences between consecutive items, built on windowed.

#iterators#generators#itertools2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Sliding Windows over Any Iterable · TechGradify