Created
October 16, 2017 00:28
-
-
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.
This file contains hidden or 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 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