Skip to content

Instantly share code, notes, and snippets.

@skyline75489
Last active August 8, 2016 10:49
Show Gist options
  • Save skyline75489/0aca8eec8dd92d5c9ba9 to your computer and use it in GitHub Desktop.
Save skyline75489/0aca8eec8dd92d5c9ba9 to your computer and use it in GitHub Desktop.
implement range in python, to mimic the builtin range
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