Fixed fib function errors not initially caught from cached values in the IPython kernel. Code cleanup.

This commit is contained in:
Donne Martin 2015-05-30 10:52:40 -04:00
parent bfd143ca81
commit b34be263cc

View File

@ -55,6 +55,17 @@
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"num_items = 10"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -67,9 +78,9 @@
" if n == 0 or n == 1:\n",
" return n\n",
" else:\n",
" return fib(n-1) + fib(n-2)\n",
" return fib_recursive(n-1) + fib_recursive(n-2)\n",
"\n",
"for i in xrange(0, 10):\n",
"for i in xrange(0, num_items):\n",
" print(fib_recursive(i))"
]
},
@ -81,8 +92,7 @@
},
"outputs": [],
"source": [
"n = 7\n",
"cache = [None] * (n + 1)\n",
"cache = [None] * (num_items + 1)\n",
"\n",
"def fib_dynamic(n):\n",
" if n == 0 or n == 1:\n",
@ -92,7 +102,7 @@
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
" return cache[n]\n",
"\n",
"for i in xrange(0, 10):\n",
"for i in xrange(0, num_items):\n",
" print(fib_dynamic(i))"
]
},
@ -100,7 +110,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
@ -116,7 +126,7 @@
" n_minus_2, n_minus_1 = n_minus_1, total\n",
" return total\n",
"\n",
"for i in xrange(0, 10):\n",
"for i in xrange(0, num_items):\n",
" print(fib_iterative(i))"
]
}