Last active
October 14, 2016 19:21
-
-
Save sin-to-jin/01ad7d382eed97b517bcd9f9edf4666e to your computer and use it in GitHub Desktop.
Python function
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
| class Fibonacci(object): | |
| """Fibonacci function.""" | |
| def __init__(self): | |
| print 'Fibonacci' | |
| def fibonacci(self, n): | |
| if n == 0: | |
| return 0 | |
| elif n == 1: | |
| return 1 | |
| else: | |
| return self.fibonacci(n - 2) + self.fibonacci(n - 1) |
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
| import unittest | |
| import fibonacci | |
| import numpy as np | |
| ANSWER_DATA = np.array( | |
| [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]) | |
| TEST_DATA = np.array(range(22)) | |
| F = fibonacci.Fibonacci() | |
| f = lambda x: F.fibonacci(x) | |
| class MyTestCase(unittest.TestCase): | |
| def test_fibonacci(self): | |
| for x in TEST_DATA: | |
| self.assertEqual(f(x), ANSWER_DATA[x]) | |
| if __name__ == '__main__': | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment