Skip to content
Onsite

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...of and 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 step counts down, and then end is still exclusive.
  • If step points away from end, the range is empty.
  • step of 0 throws a RangeError when range is 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

Run checks the 3 visible tests, Submit all 9.