diff --git a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb index a61eae0..52384bc 100644 --- a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb @@ -34,7 +34,14 @@ "source": [ "## Constraints\n", "\n", - "* None" + "* Does the sequence start at 0 or 1?\n", + " * 0\n", + "* Can we assume the inputs are valid non-negative ints?\n", + " * Yes\n", + "* Are you looking for a recursive or iterative solution?\n", + " * Implement both\n", + "* Can we assume this fits memory?\n", + " * Yes" ] }, { @@ -45,7 +52,8 @@ "\n", "* n = 0 -> 0\n", "* n = 1 -> 1\n", - "* n > 1 -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..." + "* n = 6 -> 8\n", + "* Fib sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..." ] }, { @@ -85,11 +93,7 @@ }, "outputs": [], "source": [ - "num_items = 10\n", - "cache = [None] * (num_items + 1)\n", - "\n", - "\n", - "def fib_dynamic(n):\n", + "def fib_iterative(n):\n", " # TODO: Implement me\n", " pass" ] @@ -102,7 +106,7 @@ }, "outputs": [], "source": [ - "def fib_iterative(n):\n", + "def fib_dynamic(n):\n", " # TODO: Implement me\n", " pass" ] @@ -134,10 +138,10 @@ "\n", " def test_fib(self, func):\n", " result = []\n", - " for i in range(num_items):\n", + " expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", + " for i in range(len(expected)):\n", " result.append(func(i))\n", - " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", - " assert_equal(result, fib_seq)\n", + " assert_equal(result, expected)\n", " print('Success: test_fib')\n", "\n", "\n", @@ -164,21 +168,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb index c95948c..83a0d94 100644 --- a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb @@ -33,7 +33,14 @@ "source": [ "## Constraints\n", "\n", - "* None" + "* Does the sequence start at 0 or 1?\n", + " * 0\n", + "* Can we assume the inputs are valid non-negative ints?\n", + " * Yes\n", + "* Are you looking for a recursive or iterative solution?\n", + " * Either\n", + "* Can we assume this fits memory?\n", + " * Yes" ] }, { @@ -44,7 +51,8 @@ "\n", "* n = 0 -> 0\n", "* n = 1 -> 1\n", - "* n > 1 -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..." + "* n = 6 -> 8\n", + "* Fib sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..." ] }, { @@ -93,17 +101,12 @@ }, "outputs": [], "source": [ - "num_items = 10\n", - "cache = [None] * (num_items + 1)\n", - "\n", - "\n", - "def fib_dynamic(n):\n", - " if n == 0 or n == 1:\n", - " return n\n", - " if cache[n] != None:\n", - " return cache[n]\n", - " cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n", - " return cache[n]" + "def fib_iterative(n):\n", + " a = 0\n", + " b = 1\n", + " for _ in range(n):\n", + " a, b = b, a + b\n", + " return a" ] }, { @@ -114,12 +117,18 @@ }, "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" + "def fib_dynamic(n):\n", + " cache = {}\n", + " return _fib_dynamic(n, cache)\n", + "\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]" ] }, { @@ -154,10 +163,10 @@ "\n", " def test_fib(self, func):\n", " result = []\n", - " for i in range(num_items):\n", + " expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", + " for i in range(len(expected)):\n", " result.append(func(i))\n", - " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", - " assert_equal(result, fib_seq)\n", + " assert_equal(result, expected)\n", " print('Success: test_fib')\n", "\n", "\n", @@ -196,21 +205,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/recursion_dynamic/fibonacci/test_fibonacci.py b/recursion_dynamic/fibonacci/test_fibonacci.py index b28f0a0..fea89a0 100644 --- a/recursion_dynamic/fibonacci/test_fibonacci.py +++ b/recursion_dynamic/fibonacci/test_fibonacci.py @@ -5,10 +5,10 @@ class TestFib(object): def test_fib(self, func): result = [] - for i in range(num_items): + expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] + for i in range(len(expected)): result.append(func(i)) - fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - assert_equal(result, fib_seq) + assert_equal(result, expected) print('Success: test_fib')