mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Fixed fib function errors not initially caught from cached values in the IPython kernel. Code cleanup.
This commit is contained in:
parent
bfd143ca81
commit
b34be263cc
|
@ -55,6 +55,17 @@
|
||||||
"## Code"
|
"## Code"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"num_items = 10"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -67,9 +78,9 @@
|
||||||
" if n == 0 or n == 1:\n",
|
" if n == 0 or n == 1:\n",
|
||||||
" return n\n",
|
" return n\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" return fib(n-1) + fib(n-2)\n",
|
" return fib_recursive(n-1) + fib_recursive(n-2)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, 10):\n",
|
"for i in xrange(0, num_items):\n",
|
||||||
" print(fib_recursive(i))"
|
" print(fib_recursive(i))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -81,8 +92,7 @@
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"n = 7\n",
|
"cache = [None] * (num_items + 1)\n",
|
||||||
"cache = [None] * (n + 1)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"def fib_dynamic(n):\n",
|
"def fib_dynamic(n):\n",
|
||||||
" if n == 0 or n == 1:\n",
|
" if n == 0 or n == 1:\n",
|
||||||
|
@ -92,7 +102,7 @@
|
||||||
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
|
" cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n",
|
||||||
" return cache[n]\n",
|
" return cache[n]\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, 10):\n",
|
"for i in xrange(0, num_items):\n",
|
||||||
" print(fib_dynamic(i))"
|
" print(fib_dynamic(i))"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -100,7 +110,7 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -116,7 +126,7 @@
|
||||||
" n_minus_2, n_minus_1 = n_minus_1, total\n",
|
" n_minus_2, n_minus_1 = n_minus_1, total\n",
|
||||||
" return total\n",
|
" return total\n",
|
||||||
"\n",
|
"\n",
|
||||||
"for i in xrange(0, 10):\n",
|
"for i in xrange(0, num_items):\n",
|
||||||
" print(fib_iterative(i))"
|
" print(fib_iterative(i))"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user