Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get a Fibonacci number with O(n).
# 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