Skip to content

Instantly share code, notes, and snippets.

@gh640
Created September 3, 2017 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gh640/a69d2018801ce3578868375a060f1e78 to your computer and use it in GitHub Desktop.
Save gh640/a69d2018801ce3578868375a060f1e78 to your computer and use it in GitHub Desktop.
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