MediumPython

Generators — Range Stepper

PythonGeneratorsIterators

Implement solve(start, stop, step) as a generator function that yields integers from start up to (but not including) stop, incrementing by step each time.

Examples

  • list(solve(0, 10, 2))[0, 2, 4, 6, 8]
  • list(solve(1, 6, 1))[1, 2, 3, 4, 5]
  • list(solve(5, 0, -1))[5, 4, 3, 2, 1]
  • list(solve(0, 0, 1))[]

Constraints

  • step may be negative (count down).
  • If step == 0, raise a ValueError.
  • Do not use Python's built-in range().
  • Your function must use the yield keyword.

Sample tests

Test #1Even numbers 0-8
Input: [0,10,2]
Output: [0,2,4,6,8]
Test #2Step of 1
Input: [1,6,1]
Output: [1,2,3,4,5]
Test #3Countdown
Input: [5,0,-1]
Output: [5,4,3,2,1]
Test #4Empty range
Input: [0,0,1]
Output: []