Created
December 13, 2016 01:00
-
-
Save alanduan/7568b21cfb11207e16d73fe37417a95b to your computer and use it in GitHub Desktop.
nth fibonacci
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
import unittest | |
class FibonacciTest(unittest.TestCase): | |
def test_sequence(self): | |
self.assertEqual(fibonacci_nth(0), 0) | |
self.assertEqual(fibonacci_nth(1), 1) | |
self.assertEqual(fibonacci_nth(2), 1) | |
self.assertEqual(fibonacci_nth(3), 2) | |
self.assertEqual(fibonacci_nth(4), 3) | |
self.assertEqual(fibonacci_nth(5), 5) | |
self.assertEqual(fibonacci_nth(6), 8) | |
def test_LargerValue(self): | |
self.assertEqual(fibonacci_nth(100), 354224848179261915075) | |
def fibonacci_generator(): | |
a = 0 | |
b = 1 | |
while True: | |
yield a | |
a, b = b, a + b | |
def fibonacci_nth(n): | |
for i, v in enumerate(fibonacci_generator()): | |
if i == n: | |
return v | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment