Last active
August 8, 2016 10:49
-
-
Save skyline75489/0aca8eec8dd92d5c9ba9 to your computer and use it in GitHub Desktop.
implement range in python, to mimic the builtin range
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 my_range(start, stop=None, step=None): | |
result = [] | |
begin = 0 | |
end = 0 | |
_step = 1 | |
if stop is None: | |
end = start | |
else: | |
begin = start | |
end = stop | |
i = begin | |
if step is not None: | |
assert step != 0, "step must not be zero" | |
_step = step | |
if i<end: | |
if _step < 0: | |
return [] | |
while i<end: | |
result.append(i) | |
i += _step | |
else: | |
if _step > 0: | |
return [] | |
while i>end: | |
result.append(i) | |
i += _step | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment