The One-Shot Generator Trap

Medium ⏱ 12 min 58% acceptance ★★★★★ 4.7
Generators are consumed once: summing one twice gives 0 the second time. Write stats(nums_iterable) returning (total, count, maximum) from a possibly-one-shot iterable by materializing it once into a tuple first — the defensive move when you must traverse twice. Return (0, 0, None) for an empty input.

Examples

Example 1
Input
stats(x * 2 for x in range(3))
Output
(6, 3, 4)
Explanation

The generator was captured once, then analyzed.

Constraints

  • Consume the input exactly once.
  • Empty input → (0, 0, None).

Topics

Iterators & Generatorsexhaustion

Companies

GoogleMicrosoftAdobe

Hints

Hint 1

data = tuple(nums_iterable) up front.

Hint 2

Then sum/len/max safely on the tuple.

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