Created
August 6, 2019 06:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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