A Countdown You Can Iterate

Medium ⏱ 12 min 56% acceptance ★★★★★ 4.9
Implement Countdown(start) as its own iterator: __iter__ returns self and __next__ yields start, start-1, … 1 then raises StopIteration. Then list(Countdown(3)) is [3, 2, 1]. (The generator version belongs to another topic — here build the protocol by hand.)

Examples

Example 1
Input
list(Countdown(3))
Output
[3, 2, 1]
Explanation

The for machinery calls __next__ until StopIteration.

Constraints

  • No generators — implement both dunders.
  • Iterating a second time yields nothing (exhausted), which is standard iterator behavior.

Topics

OOP__iter__ and __next__

Companies

SpaceXISRO VendorsHoneywell

Hints

Hint 1

Track the current value as instance state.

Hint 2

raise StopIteration when it hits 0.

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