From c681d5a28ead377ea2b82adaaf18f1c6e224f2e0 Mon Sep 17 00:00:00 2001 From: Chance Nelson Date: Tue, 1 Oct 2019 23:58:15 -0700 Subject: [PATCH 1/3] add example functions for fibonacci sequence and memoization --- allalgorithms/numeric/fibonacci.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 allalgorithms/numeric/fibonacci.py diff --git a/allalgorithms/numeric/fibonacci.py b/allalgorithms/numeric/fibonacci.py new file mode 100644 index 0000000..e875413 --- /dev/null +++ b/allalgorithms/numeric/fibonacci.py @@ -0,0 +1,44 @@ +# -*- coding: UTF-8 -*- +# +# Numeric Algorithms: Factorials and Memoization +# The All â–²lgorithms library for python +# +# Contributed by: Chance +# Github: @chance-nelson +# + + +def fibonacci(n): + """Compute the Fibonacci sequence number at a certain index + + This is the obvious recursive approach, and runs on the order of O(n*2). + """ + # Base case 1: fibonacci(1) == 0 + if n == 1: + return 0 + + # Base case 2: fibonacci(2) == 1 + elif n == 2: + return 1 + + return fibonacci(n-1) + fibonacci(n-2) + + +def memoize(f): + """Special decorator to add memoization to any recursive function + + Keep a cache of results from past runs. This cache can then be referenced + instead of calling the function again. + """ + memory = {} + + def inner(num): + if num not in memory: + memory[num] = f(num) + + return memory[num] + + return inner + + +fibonacci_memo = memoize(fibonacci) From 64cf5f66ca00dfa13ef4f13d2dece2a6c95b1391 Mon Sep 17 00:00:00 2001 From: Chance Nelson Date: Tue, 1 Oct 2019 23:58:42 -0700 Subject: [PATCH 2/3] add tests for fibonacci functions --- tests/test_numeric.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_numeric.py b/tests/test_numeric.py index 2be532d..a0c9a3f 100644 --- a/tests/test_numeric.py +++ b/tests/test_numeric.py @@ -1,6 +1,7 @@ import unittest from allalgorithms.numeric import find_max +from allalgorithms.fibonacci import fibonacci, fibonacci_memo class TestMax(unittest.TestCase): @@ -9,5 +10,15 @@ def test_find_max_value(self): test_list = [3, 1, 8, 7, 4] self.assertEqual(8, find_max(test_list)) + +class TestFibonacci(unittest.TestCase): + + def test_recursive_fibonacci(self): + self.assertEqual(8, fibonacci(7)) + + def test_memoize_fibonacci(self): + self.assertEqual(8, fibonacci(7)) + + if __name__ == "__main__": unittest.main() From 47993f0aa122eee438cf14861d3738b407cd7454 Mon Sep 17 00:00:00 2001 From: Chance Nelson Date: Tue, 1 Oct 2019 23:59:01 -0700 Subject: [PATCH 3/3] add fibonacci import --- allalgorithms/numeric/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/allalgorithms/numeric/__init__.py b/allalgorithms/numeric/__init__.py index cd1f5cc..5dfa7ad 100644 --- a/allalgorithms/numeric/__init__.py +++ b/allalgorithms/numeric/__init__.py @@ -1 +1,2 @@ -from .max_numbers import find_max \ No newline at end of file +from .max_numbers import find_max +from .fibonacci import fibonacci, fibonacci_memo