easy +10 pts

Iterator protocol class

Build a custom iterator class that lazily yields squared values until a stop value.

Create a class named `SquareIterator` that implements the iterator protocol (i.e., has `__iter__` and `__next__` methods). When instantiated with an integer argument `stop`, an instance should produce a lazy sequence of squares: `0², 1², 2², ..., n²` where `n` is the largest integer such that `n² < stop`. The sequence stops before reaching or exceeding the value of `stop`. Your class must: - Accept exactly one positional argument `stop` (an integer). - Be an iterable (so `iter(obj)` returns the object). - Be an iterator (so `next(obj)` works). - Raise `StopIteration` automatically when the sequence is exhausted. You may assume `stop` is a non-negative integer. Implement the `SquareIterator` class with the required methods.

Constraints

- `0 <= stop <= 10**6` - Must not use generator functions or `yield` in the class implementation. - Must be memory-efficient: do not precompute the entire list of squares.

Example

>>> obj = SquareIterator(5)
>>> list(obj)
[0, 1, 4]
>>> obj = SquareIterator(1)
>>> list(obj)
[0]
>>> obj = SquareIterator(0)
>>> list(obj)
[]
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Track the current integer with an internal attribute, starting at 0.
In `__next__`, compute the square and compare it to `stop`; raise `StopIteration` when the square is no longer less than `stop`.
Remember that `__iter__` should return `self`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.