Skip to content

Commit 4e98b47

Browse files
committed
update Ch13-Recursion
1 parent ee98f95 commit 4e98b47

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

notebooks/Ch13-Recursion.ipynb

+67-16
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,15 @@
267267
},
268268
{
269269
"cell_type": "code",
270-
"execution_count": null,
270+
"execution_count": 8,
271271
"metadata": {},
272272
"outputs": [],
273273
"source": [
274274
"# Finding Fibonacci number in series\n",
275-
"# count = 0\n",
275+
"count = 0\n",
276276
"def fib(n):\n",
277-
" #global count\n",
278-
" #count += 1\n",
277+
" global count\n",
278+
" count += 1\n",
279279
" if n <= 1:\n",
280280
" return n\n",
281281
" f = fib(n-1) + fib(n-2)\n",
@@ -284,14 +284,22 @@
284284
},
285285
{
286286
"cell_type": "code",
287-
"execution_count": null,
287+
"execution_count": 9,
288288
"metadata": {},
289-
"outputs": [],
289+
"outputs": [
290+
{
291+
"name": "stdout",
292+
"output_type": "stream",
293+
"text": [
294+
"fib was called 7049155 times.\n"
295+
]
296+
}
297+
],
290298
"source": [
291299
"fib(32)\n",
292-
"#print(count)\n",
293-
"#assert fib(8) == 21\n",
294-
"#assert fib(10) == 55"
300+
"print(f'fib was called {count} times.')\n",
301+
"assert fib(8) == 21\n",
302+
"assert fib(10) == 55"
295303
]
296304
},
297305
{
@@ -301,14 +309,38 @@
301309
"<img src=\"./resources/recursion-fib.svg\" width=\"50%\">\n",
302310
"\n",
303311
"### visualize fib(4) using pythontutor.com\n",
304-
"- https://goo.gl/YNizhH"
312+
"- https://goo.gl/YNizhH\n",
313+
"\n",
314+
"### You can timeit\n",
315+
"\n",
316+
"- use timeit function to see how long fib(n) takes"
305317
]
306318
},
307319
{
308320
"cell_type": "code",
309-
"execution_count": null,
321+
"execution_count": 10,
310322
"metadata": {},
311323
"outputs": [],
324+
"source": [
325+
"from timeit import timeit"
326+
]
327+
},
328+
{
329+
"cell_type": "code",
330+
"execution_count": 11,
331+
"metadata": {},
332+
"outputs": [
333+
{
334+
"data": {
335+
"text/plain": [
336+
"1.1521884420071729"
337+
]
338+
},
339+
"execution_count": 11,
340+
"metadata": {},
341+
"output_type": "execute_result"
342+
}
343+
],
312344
"source": [
313345
"timeit(\"fib(32)\", globals=globals(), number=1)"
314346
]
@@ -325,7 +357,7 @@
325357
},
326358
{
327359
"cell_type": "code",
328-
"execution_count": null,
360+
"execution_count": 12,
329361
"metadata": {},
330362
"outputs": [],
331363
"source": [
@@ -339,18 +371,37 @@
339371
},
340372
{
341373
"cell_type": "code",
342-
"execution_count": null,
374+
"execution_count": 13,
343375
"metadata": {},
344-
"outputs": [],
376+
"outputs": [
377+
{
378+
"name": "stdout",
379+
"output_type": "stream",
380+
"text": [
381+
"2178309\n"
382+
]
383+
}
384+
],
345385
"source": [
346386
"print(fib_tail(32, 0, 1))"
347387
]
348388
},
349389
{
350390
"cell_type": "code",
351-
"execution_count": null,
391+
"execution_count": 14,
352392
"metadata": {},
353-
"outputs": [],
393+
"outputs": [
394+
{
395+
"data": {
396+
"text/plain": [
397+
"1.3885000953450799e-05"
398+
]
399+
},
400+
"execution_count": 14,
401+
"metadata": {},
402+
"output_type": "execute_result"
403+
}
404+
],
354405
"source": [
355406
"timeit(\"fib_tail(32, 0, 1)\", globals=globals(), number=1)"
356407
]

0 commit comments

Comments
 (0)