Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created October 16, 2017 00:28
Show Gist options
  • Save whalesalad/8ba65cd128f26baa00a172046ac15267 to your computer and use it in GitHub Desktop.
Save whalesalad/8ba65cd128f26baa00a172046ac15267 to your computer and use it in GitHub Desktop.
Randomly had the urge to write a Fibonacci sequence backed by a python generator.
def fib():
x = 0
y = 1
while True:
yield x
x, y = y, x + y
def take(n, coll):
return [x[1] for x in zip(range(n), coll)]
if __name__ == '__main__':
# Make sure that take() works.
assert take(3, [0, 1, 2, 3, 4, 5]) == [0, 1, 2]
outcome = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
# Ensure our fib sequence works!
assert take(len(outcome), f) == outcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment