Skip to content

Instantly share code, notes, and snippets.

@BenMaydan
Created August 6, 2019 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMaydan/c3e589cbfbd3e449a80ef7bfcf23f061 to your computer and use it in GitHub Desktop.
Save BenMaydan/c3e589cbfbd3e449a80ef7bfcf23f061 to your computer and use it in GitHub Desktop.
Exact range replica. I wanted to learn about python generators so I made a range replica
def new_range(start, stop, step=1):
"""
A copy of the normal python range generator
:param start: The number to start at
:param stop: The number to stop at (function stops at stop - step)
:param step: start + step until stop - step is reached
:return: Current number
"""
# If the resulting logic is an infinitely long generator (start can never reach stop - step)
if (start < stop and step < 0) or (start > stop and step > 0):
return
# Iterates start value positively
if start < stop:
while start < stop:
yield start
start += step
# Iterates start value negatively
elif start > stop:
while start > stop:
yield start
start += step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment