easy +8 pts

Range-like generator

Build a lazy range generator with steps and bounds.

Write a generator function `custom_range(start, stop=None, step=1)` that lazily produces numbers similarly to Python's built-in `range`, but with the following semantics: - If `stop` is `None`, `start` is treated as the stop value and the sequence starts at 0. - Otherwise, `start` is the inclusive start and `stop` is the exclusive stop. - `step` must be non-zero. If `step` is positive, the sequence increases from start to less than stop. If `step` is negative, the sequence decreases from start to greater than stop. - The generator must yield values one by one (do not return a list). - The sequence may be empty if start/stop do not align with the step direction. Implement the function so that it can be used in a for loop or with `list()`.

Constraints

- `start`, `stop`, and `step` are integers. - `step` is never zero. - The generator only needs to handle typical integer ranges; no overflow concerns.

Example

>>> list(custom_range(5))
[0, 1, 2, 3, 4]
>>> list(custom_range(2, 8, 2))
[2, 4, 6]
>>> list(custom_range(10, 0, -3))
[10, 7, 4, 1]
>>> list(custom_range(3, 3))
[]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Set start/stop properly when stop is None.
Think about whether to increment or decrement based on step's sign.
Use a while loop with a condition that depends on step's sign.
Remember to yield inside the loop, not accumulate a list.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.