This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
class Math(object):
def fib_iterative(self, n):
# TODO: Implement me
pass
def fib_recursive(self, n):
# TODO: Implement me
pass
def fib_dynamic(self, n):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_fibonacci.py
import unittest
class TestFib(unittest.TestCase):
def test_fib(self, func):
result = []
expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
for i in range(len(expected)):
result.append(func(i))
self.assertEqual(result, expected)
print('Success: test_fib')
def main():
test = TestFib()
math = Math()
test.test_fib(math.fib_recursive)
test.test_fib(math.fib_dynamic)
test.test_fib(math.fib_iterative)
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.