Implement an iterable range(start, end, step)
EasyP3Coding
Implement range(start, end, step = 1), which returns an iterable of numbers from start up to but not including end, moving by step.
- It must work with
for...ofand spread ([...range(0, 3)]). - It is lazy: values are produced on demand, so
range(0, Infinity)is allowed when the consumer stops early. - A negative
stepcounts down, and thenendis still exclusive. - If
steppoints away fromend, the range is empty. stepof0throws aRangeErrorwhenrangeis called.- The returned value can be iterated more than once, each time from the start.
Example 1
- Input
- [...range(0, 5)]
- Output
- [0, 1, 2, 3, 4]
Example 2
- Input
- [...range(10, 0, -3)]
- Output
- [10, 7, 4, 1]
- Why
- A negative step counts down and stops before reaching 0.
Constraints
- start, end and step are numbers; end may be Infinity or -Infinity.
- Do not build an array of the values up front.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.