From 91b3efc3e40a8c12df28d00620312c309476dad0 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sun, 22 Jan 2017 12:06:58 -0500 Subject: [PATCH] Move Fibonacci challenge code to a class (#140) --- .../fibonacci/fibonacci_challenge.ipynb | 51 +++++------- .../fibonacci/fibonacci_solution.ipynb | 78 ++++++++----------- recursion_dynamic/fibonacci/test_fibonacci.py | 7 +- 3 files changed, 53 insertions(+), 83 deletions(-) diff --git a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb index 345aafd..6907fd6 100644 --- a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb @@ -76,39 +76,23 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "def fib_recursive(n):\n", - " # TODO: Implement me\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def fib_iterative(n):\n", - " # TODO: Implement me\n", - " pass" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def fib_dynamic(n):\n", - " # TODO: Implement me\n", - " pass" + "class Math(object):\n", + "\n", + " def fib_iterative(self, n):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def fib_recursive(self, n):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def fib_dynamic(self, n):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -147,9 +131,10 @@ "\n", "def main():\n", " test = TestFib()\n", - " test.test_fib(fib_recursive)\n", - " test.test_fib(fib_dynamic)\n", - " test.test_fib(fib_iterative)\n", + " math = Math()\n", + " test.test_fib(math.fib_recursive)\n", + " test.test_fib(math.fib_dynamic)\n", + " test.test_fib(math.fib_iterative)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb index a049319..a026e87 100644 --- a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb @@ -82,53 +82,36 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "def fib_recursive(n):\n", - " if n == 0 or n == 1:\n", - " return n\n", - " else:\n", - " return fib_recursive(n-1) + fib_recursive(n-2)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def fib_iterative(n):\n", - " a = 0\n", - " b = 1\n", - " for _ in range(n):\n", - " a, b = b, a + b\n", - " return a" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def fib_dynamic(n):\n", - " cache = {}\n", - " return _fib_dynamic(n, cache)\n", + "class Math(object):\n", "\n", + " def fib_iterative(self, n):\n", + " a = 0\n", + " b = 1\n", + " for _ in range(n):\n", + " a, b = b, a + b\n", + " return a\n", "\n", - "def _fib_dynamic(n, cache):\n", - " if n == 0 or n == 1:\n", - " return n\n", - " if n in cache:\n", - " return cache[n]\n", - " cache[n] = _fib_dynamic(n-1, cache) + _fib_dynamic(n-2, cache)\n", - " return cache[n]" + " def fib_recursive(self, n):\n", + " if n == 0 or n == 1:\n", + " return n\n", + " else:\n", + " return self.fib_recursive(n-1) + self.fib_recursive(n-2)\n", + "\n", + " def fib_dynamic(self, n):\n", + " cache = {}\n", + " return self._fib_dynamic(n, cache)\n", + "\n", + " def _fib_dynamic(self, n, cache):\n", + " if n == 0 or n == 1:\n", + " return n\n", + " if n in cache:\n", + " return cache[n]\n", + " cache[n] = self._fib_dynamic(n-1, cache) + self._fib_dynamic(n-2, cache)\n", + " return cache[n]" ] }, { @@ -141,7 +124,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -172,9 +155,10 @@ "\n", "def main():\n", " test = TestFib()\n", - " test.test_fib(fib_recursive)\n", - " test.test_fib(fib_dynamic)\n", - " test.test_fib(fib_iterative)\n", + " math = Math()\n", + " test.test_fib(math.fib_recursive)\n", + " test.test_fib(math.fib_dynamic)\n", + " test.test_fib(math.fib_iterative)\n", "\n", "\n", "if __name__ == '__main__':\n", @@ -183,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": { "collapsed": false }, diff --git a/recursion_dynamic/fibonacci/test_fibonacci.py b/recursion_dynamic/fibonacci/test_fibonacci.py index fea89a0..fed4907 100644 --- a/recursion_dynamic/fibonacci/test_fibonacci.py +++ b/recursion_dynamic/fibonacci/test_fibonacci.py @@ -14,9 +14,10 @@ class TestFib(object): def main(): test = TestFib() - test.test_fib(fib_recursive) - test.test_fib(fib_dynamic) - test.test_fib(fib_iterative) + math = Math() + test.test_fib(math.fib_recursive) + test.test_fib(math.fib_dynamic) + test.test_fib(math.fib_iterative) if __name__ == '__main__':