Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Last active August 29, 2015 13:56
Show Gist options
  • Save danielrobertson/9236021 to your computer and use it in GitHub Desktop.
Save danielrobertson/9236021 to your computer and use it in GitHub Desktop.
My idea of what Python 3's range(begin, end, step) looks like
def range(begin, end = None, step = 1):
if step == 0:
raise Exception("Step size cannot be 0")
if end == None:
end, begin = begin, 0
while (abs(begin + step - end)) <= (abs(begin - end)):
yield begin
begin += step
assert(list(range(10)) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
assert(list(range(0,10)) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
assert(list(range(10, 0, 1)) == [])
assert(list(range(0, 10, 1)) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
assert(list(range(0, 10, -1)) == [])
assert(list(range(10, 0, -1)) == [10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment