Skip to content

Commit fca7c95

Browse files
committed
publish Ch 31
1 parent 5cc5501 commit fca7c95

File tree

4 files changed

+141
-4
lines changed

4 files changed

+141
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
assets/.DS_Store
33
.DS_Store
4+
/assets/notebooks/.ipynb_checkpoints
1000 KB
Binary file not shown.

assets/notebooks/31_recursion.ipynb

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 31. Factorials\n",
8+
"\n",
9+
"## Recursive functions\n",
10+
"\n",
11+
"[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n",
12+
"Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n",
13+
"\n",
14+
"---"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"- Write a function that calculates the factorial of a given integer using a for loop:"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def factorial_for (n):\n",
31+
" \"\"\"Calculates the factorial of a given integer using a for loop\n",
32+
"\n",
33+
" Parameters\n",
34+
" ----------\n",
35+
" n : integer\n",
36+
" The input integer \n",
37+
"\n",
38+
" Returns\n",
39+
" -------\n",
40+
" factorial : integer \n",
41+
" The factorial of the input integer\n",
42+
" \"\"\"\n",
43+
"\n",
44+
" # initialize the result to one\n",
45+
" factorial = 1\n",
46+
"\n",
47+
" # for each integer between 2 and the input integer\n",
48+
" for i in range (2, n+1):\n",
49+
" # multiply the current result for the current integer \n",
50+
" factorial *= i\n",
51+
"\n",
52+
" # return the result\n",
53+
" return factorial\n",
54+
"\n",
55+
"# call the function\n",
56+
"fact = factorial_for (4)\n",
57+
"print (fact)"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"- Compare the previous iterative function with the following recursive function:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"def factorial_rec (n):\n",
74+
" \"\"\"Calculates the factorial of a given integer using recursion\n",
75+
"\n",
76+
" Parameters\n",
77+
" ----------\n",
78+
" n : integer\n",
79+
" The input integer \n",
80+
"\n",
81+
" Returns\n",
82+
" -------\n",
83+
" integer \n",
84+
" The factorial of the input integer\n",
85+
" \"\"\"\n",
86+
" \n",
87+
" # if n is greater than 1\n",
88+
" if n > 1: \n",
89+
" # execute the recursion\n",
90+
" return factorial_rec(n-1) * n\n",
91+
" # otherwise\n",
92+
" else: \n",
93+
" # return 1\n",
94+
" return 1 \n",
95+
"\n",
96+
"# call the function\n",
97+
"fact = factorial_rec (4)\n",
98+
"print (fact)"
99+
]
100+
}
101+
],
102+
"metadata": {
103+
"kernelspec": {
104+
"display_name": "Python 3 (ipykernel)",
105+
"language": "python",
106+
"name": "python3"
107+
},
108+
"language_info": {
109+
"codemirror_mode": {
110+
"name": "ipython",
111+
"version": 3
112+
},
113+
"file_extension": ".py",
114+
"mimetype": "text/x-python",
115+
"name": "python",
116+
"nbconvert_exporter": "python",
117+
"pygments_lexer": "ipython3",
118+
"version": "3.9.6"
119+
},
120+
"widgets": {
121+
"application/vnd.jupyter.widget-state+json": {
122+
"state": {},
123+
"version_major": 2,
124+
"version_minor": 0
125+
}
126+
}
127+
},
128+
"nbformat": 4,
129+
"nbformat_minor": 4
130+
}

index.html

+10-4
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ <h2>About the Book</h2>
235235
<div class="col-lg-12">
236236

237237
<div class="progress">
238-
<span class="skill">BOOK COMPLETION <i class="val">80%</i></span>
238+
<span class="skill">BOOK COMPLETION <i class="val">83%</i></span>
239239
<div class="progress-bar-wrap">
240-
<div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100"></div>
240+
<div class="progress-bar" role="progressbar" aria-valuenow="83" aria-valuemin="0" aria-valuemax="100"></div>
241241
</div>
242242
</div>
243-
<center>Next upload: <b>Chapter 31</b> on <b>March 3</b> </center>
243+
<center>Next upload: <b>Chapter 32</b> on <b>April 2</b> </center>
244244
</div>
245245

246246
</div>
@@ -350,15 +350,21 @@ <h4>PART 8: Functions </h4>
350350
<a href="assets/notebooks/28_function_inputs.ipynb" onclick="download" ><li>Printing Thank you cards<br> <i>Function inputs</i></li></a>
351351
<a href="assets/notebooks/29_function_outputs.ipynb" onclick="download" ><li>Login database for an online store<br> <i>Function outputs and modular design</i></li></a>
352352
<a href="assets/notebooks/30_function_io_extra.ipynb" onclick="download" ><li>Free ticket at the museum<br> <i>Input validation and output variations</i></li></a>
353-
<li>Factorials<br> <i>Recursive functions</i></li>
353+
<a href="assets/notebooks/31_recursion.ipynb" onclick="download" ><li>Factorials<br> <i>Recursive functions</i></li></a>
354354
<li>Jupyter notebooks and modules<br> <i>Working with Jupyter Notebook and an IDE</i></li>
355+
356+
<!-- <a href="assets/notebooks/30_function_io_extra.ipynb" onclick="download" ><li>Jupyter notebooks and modules<br> <i>Working with Jupyter Notebook and an IDE</i></a>
357+
<br>(additional file: <a href="#" onclick="download" >module</a>)
358+
</li> -->
359+
355360
</ol>
356361
</div>
357362

358363
<div class="resume-item pb-0">
359364
<h4>PART 9: Misc </h4>
360365
<ol start="33">
361366
<li>Online shopping<br> <i>Reading and writing text files</i></li>
367+
(additional files: input data, output data)
362368
<li>Final Python tricks<br> <i>What's more in Python</i></li>
363369
</ol>
364370
</div>

0 commit comments

Comments
 (0)