Created
September 3, 2017 04:58
-
-
Save gh640/a69d2018801ce3578868375a060f1e78 to your computer and use it in GitHub Desktop.
Get a Fibonacci number with O(n).
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
# coding: utf-8 | |
"""Gets Fibonacci numbers with a linear Order. | |
""" | |
def get_fibonacci(n): | |
a = 0 | |
b = 1 | |
for i in range(0, n): | |
a, b = b, a + b | |
return a | |
if __name__ == '__main__': | |
import unittest | |
class FibonacciTestCase(unittest.TestCase): | |
def test_get_fibonacci(self): | |
pairs = [ | |
[0, 0], | |
[1, 1], | |
[2, 1], | |
[3, 2], | |
[4, 3], | |
[5, 5], | |
[6, 8], | |
[7, 13], | |
[8, 21], | |
] | |
for n, expected in pairs: | |
self.assertEqual(get_fibonacci(n), expected) | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment