Skip to content

Commit ee98f95

Browse files
committed
update Ch13-Recursion
1 parent 2befb55 commit ee98f95

File tree

1 file changed

+55
-165
lines changed

1 file changed

+55
-165
lines changed

notebooks/Ch13-Recursion.ipynb

+55-165
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"metadata": {},
8383
"outputs": [],
8484
"source": [
85-
"sys.setrecursionlimit(1000000)"
85+
"sys.setrecursionlimit(1_000_000)"
8686
]
8787
},
8888
{
@@ -164,135 +164,83 @@
164164
"cell_type": "markdown",
165165
"metadata": {},
166166
"source": [
167-
"### Factorial definition\n",
167+
"### Sum of First N positive integers\n",
168168
"\n",
169-
"```\n",
170-
" 1! = 1 (base case)\n",
171-
" n! = n.(n-1)! for n > 1 (general case)\n",
172-
"```\n",
169+
"- use recursion to find the sum of first N positive integers\n",
173170
"\n",
174-
"- Exercise - Implement factorial recursive solution"
171+
"```\n",
172+
" first_sum(1) = 1 (base case)\n",
173+
" first_sum(n) = n + first_sum(n-1) for n > 1 (general case)\n",
174+
"```"
175175
]
176176
},
177177
{
178178
"cell_type": "code",
179-
"execution_count": 7,
179+
"execution_count": 1,
180180
"metadata": {},
181181
"outputs": [],
182182
"source": [
183-
"def factorial(n):\n",
183+
"def first_sum(n):\n",
184184
" if n == 1:\n",
185185
" return 1\n",
186186
" else:\n",
187-
" return n*factorial(n-1)"
187+
" return n + first_sum(n-1)"
188188
]
189189
},
190190
{
191191
"cell_type": "code",
192-
"execution_count": 8,
193-
"metadata": {},
194-
"outputs": [
195-
{
196-
"data": {
197-
"text/plain": [
198-
"93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
199-
]
200-
},
201-
"execution_count": 8,
202-
"metadata": {},
203-
"output_type": "execute_result"
204-
}
205-
],
206-
"source": [
207-
"factorial(100)"
208-
]
209-
},
210-
{
211-
"cell_type": "code",
212-
"execution_count": 9,
213-
"metadata": {},
214-
"outputs": [],
215-
"source": [
216-
"from timeit import timeit"
217-
]
218-
},
219-
{
220-
"cell_type": "code",
221-
"execution_count": 15,
192+
"execution_count": 2,
222193
"metadata": {},
223194
"outputs": [
224195
{
225196
"data": {
226197
"text/plain": [
227-
"0.032029791036620736"
198+
"5050"
228199
]
229200
},
230-
"execution_count": 15,
201+
"execution_count": 2,
231202
"metadata": {},
232203
"output_type": "execute_result"
233204
}
234205
],
235206
"source": [
236-
"# run it \n",
237-
"timeit('factorial(10_000)', globals=globals(), number=1)"
207+
"first_sum(100)"
238208
]
239209
},
240210
{
241211
"cell_type": "code",
242-
"execution_count": 11,
212+
"execution_count": 3,
243213
"metadata": {},
244214
"outputs": [],
245215
"source": [
246216
"# use tail recursion optimization\n",
247217
"# Python doesn't optimize tail recursion \n",
248218
"# read the blog by Guido van Rossum - http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html\n",
249-
"def factorial_tail(n, running_tot=1):\n",
219+
"def sum_tail(n, running_tot=1):\n",
250220
" if n == 1:\n",
251221
" return running_tot\n",
252222
" else:\n",
253-
" return factorial_tail(n-1, running_tot*n)"
223+
" return sum_tail(n-1, running_tot+n)"
254224
]
255225
},
256226
{
257227
"cell_type": "code",
258-
"execution_count": 12,
259-
"metadata": {},
260-
"outputs": [
261-
{
262-
"data": {
263-
"text/plain": [
264-
"93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"
265-
]
266-
},
267-
"execution_count": 12,
268-
"metadata": {},
269-
"output_type": "execute_result"
270-
}
271-
],
272-
"source": [
273-
"factorial_tail(100, 1)"
274-
]
275-
},
276-
{
277-
"cell_type": "code",
278-
"execution_count": 14,
228+
"execution_count": 6,
279229
"metadata": {},
280230
"outputs": [
281231
{
282232
"data": {
283233
"text/plain": [
284-
"0.07616704609245062"
234+
"5050"
285235
]
286236
},
287-
"execution_count": 14,
237+
"execution_count": 6,
288238
"metadata": {},
289239
"output_type": "execute_result"
290240
}
291241
],
292242
"source": [
293-
"# in fact it takes longer for Python as tail recurion is not optimized\n",
294-
"# stack usage is O(n)\n",
295-
"timeit('factorial_tail(10_000, 1)', globals=globals(), number=1)"
243+
"sum_tail(100, 1)"
296244
]
297245
},
298246
{
@@ -319,7 +267,7 @@
319267
},
320268
{
321269
"cell_type": "code",
322-
"execution_count": 16,
270+
"execution_count": null,
323271
"metadata": {},
324272
"outputs": [],
325273
"source": [
@@ -336,20 +284,9 @@
336284
},
337285
{
338286
"cell_type": "code",
339-
"execution_count": 17,
287+
"execution_count": null,
340288
"metadata": {},
341-
"outputs": [
342-
{
343-
"data": {
344-
"text/plain": [
345-
"2178309"
346-
]
347-
},
348-
"execution_count": 17,
349-
"metadata": {},
350-
"output_type": "execute_result"
351-
}
352-
],
289+
"outputs": [],
353290
"source": [
354291
"fib(32)\n",
355292
"#print(count)\n",
@@ -369,20 +306,9 @@
369306
},
370307
{
371308
"cell_type": "code",
372-
"execution_count": 18,
309+
"execution_count": null,
373310
"metadata": {},
374-
"outputs": [
375-
{
376-
"data": {
377-
"text/plain": [
378-
"0.5792814260348678"
379-
]
380-
},
381-
"execution_count": 18,
382-
"metadata": {},
383-
"output_type": "execute_result"
384-
}
385-
],
311+
"outputs": [],
386312
"source": [
387313
"timeit(\"fib(32)\", globals=globals(), number=1)"
388314
]
@@ -399,7 +325,7 @@
399325
},
400326
{
401327
"cell_type": "code",
402-
"execution_count": 19,
328+
"execution_count": null,
403329
"metadata": {},
404330
"outputs": [],
405331
"source": [
@@ -413,41 +339,36 @@
413339
},
414340
{
415341
"cell_type": "code",
416-
"execution_count": 22,
342+
"execution_count": null,
417343
"metadata": {},
418-
"outputs": [
419-
{
420-
"name": "stdout",
421-
"output_type": "stream",
422-
"text": [
423-
"2178309\n"
424-
]
425-
}
426-
],
344+
"outputs": [],
427345
"source": [
428346
"print(fib_tail(32, 0, 1))"
429347
]
430348
},
431349
{
432350
"cell_type": "code",
433-
"execution_count": 24,
351+
"execution_count": null,
434352
"metadata": {},
435-
"outputs": [
436-
{
437-
"data": {
438-
"text/plain": [
439-
"1.3248994946479797e-05"
440-
]
441-
},
442-
"execution_count": 24,
443-
"metadata": {},
444-
"output_type": "execute_result"
445-
}
446-
],
353+
"outputs": [],
447354
"source": [
448355
"timeit(\"fib_tail(32, 0, 1)\", globals=globals(), number=1)"
449356
]
450357
},
358+
{
359+
"cell_type": "markdown",
360+
"metadata": {},
361+
"source": [
362+
"### Factorial definition\n",
363+
"\n",
364+
"```\n",
365+
" 1! = 1 (base case)\n",
366+
" n! = n * (n-1)! for n > 1 (general case)\n",
367+
"```\n",
368+
"\n",
369+
"- Exercise - Implement factorial recursive solution; see homework assignment!"
370+
]
371+
},
451372
{
452373
"cell_type": "markdown",
453374
"metadata": {},
@@ -469,18 +390,9 @@
469390
},
470391
{
471392
"cell_type": "code",
472-
"execution_count": 3,
393+
"execution_count": null,
473394
"metadata": {},
474-
"outputs": [
475-
{
476-
"name": "stdout",
477-
"output_type": "stream",
478-
"text": [
479-
"Requirement already up-to-date: pip in /Users/rbasnet/miniconda3/lib/python3.7/site-packages (19.2.1)\n",
480-
"Requirement already satisfied: pygame in /Users/rbasnet/miniconda3/lib/python3.7/site-packages (1.9.4)\n"
481-
]
482-
}
483-
],
395+
"outputs": [],
484396
"source": [
485397
"%%bash\n",
486398
"pip install --upgrade pip\n",
@@ -489,18 +401,9 @@
489401
},
490402
{
491403
"cell_type": "code",
492-
"execution_count": 1,
404+
"execution_count": null,
493405
"metadata": {},
494-
"outputs": [
495-
{
496-
"name": "stdout",
497-
"output_type": "stream",
498-
"text": [
499-
"pygame 1.9.4\n",
500-
"Hello from the pygame community. https://www.pygame.org/contribute.html\n"
501-
]
502-
}
503-
],
406+
"outputs": [],
504407
"source": [
505408
"# animated fractal; when done just close the window or force kill if not responding!\n",
506409
"# this program doesn't run in colab or online services. You must run locally from notebook or as a script!\n",
@@ -613,7 +516,7 @@
613516
},
614517
{
615518
"cell_type": "code",
616-
"execution_count": 5,
519+
"execution_count": null,
617520
"metadata": {},
618521
"outputs": [],
619522
"source": [
@@ -626,25 +529,11 @@
626529
},
627530
{
628531
"cell_type": "code",
629-
"execution_count": 6,
532+
"execution_count": null,
630533
"metadata": {
631534
"scrolled": true
632535
},
633-
"outputs": [
634-
{
635-
"name": "stdout",
636-
"output_type": "stream",
637-
"text": [
638-
"Move disk #1 from needle1 to needle3\n",
639-
"Move disk #2 from needle1 to needle2\n",
640-
"Move disk #1 from needle3 to needle2\n",
641-
"Move disk #3 from needle1 to needle3\n",
642-
"Move disk #1 from needle2 to needle1\n",
643-
"Move disk #2 from needle2 to needle3\n",
644-
"Move disk #1 from needle1 to needle3\n"
645-
]
646-
}
647-
],
536+
"outputs": [],
648537
"source": [
649538
"moveDisks(3, 'needle1', 'needle2', 'needle3')"
650539
]
@@ -659,8 +548,9 @@
659548
"\n",
660549
"- the following Kattis problems can be solved using recursion:\n",
661550
"\n",
662-
"1. Watch Out For Those Hailstones! - https://open.kattis.com/problems/hailstone\n",
663-
"2. Of of Sorts - https://open.kattis.com/problems/outofsorts\n",
551+
"1. Last Factorial Digit: https://open.kattis.com/problems/lastfactorialdigit\n",
552+
"2. Watch Out For Those Hailstones! - https://open.kattis.com/problems/hailstone\n",
553+
"3. Of of Sorts - https://open.kattis.com/problems/outofsorts\n",
664554
" - Hint: implement recursive binary search and apply to the generated unsorted sequence"
665555
]
666556
},

0 commit comments

Comments
 (0)