Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 77351f9

Browse files
committed
chapter5-modules(new added)
1 parent 73e8c01 commit 77351f9

File tree

4 files changed

+1528
-0
lines changed

4 files changed

+1528
-0
lines changed

book/module/Exercises/01.ipynb

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {
7+
"nbgrader": {
8+
"grade": false,
9+
"locked": true,
10+
"solution": false
11+
}
12+
},
13+
"source": [
14+
"# Exercises"
15+
]
16+
},
17+
{
18+
"attachments": {},
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"::: {note}\n",
23+
"You don't need to worry about using Interactive Jupyter Notebooks for these exercises. You can simply ignore the rocket icon ({fa}`rocket`) and go ahead to answer the questions using the knowledge you've gained from the chapter. Have fun!\n",
24+
"::: "
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 53,
30+
"metadata": {
31+
"tags": [
32+
"remove-input"
33+
]
34+
},
35+
"outputs": [],
36+
"source": [
37+
"# jupyterquiz-import\n",
38+
"#import json\n",
39+
"#from jupyterquiz import display_quiz\n",
40+
"#load all questions json file\n",
41+
"#with open(\"01.json\", \"r\") as file:\n",
42+
"# questions = json.load(file)\n",
43+
"\n",
44+
"#questions = [[q] for q in questions]"
45+
]
46+
},
47+
{
48+
"attachments": {},
49+
"cell_type": "markdown",
50+
"metadata": {
51+
"id": "su7ODKnqB7tv",
52+
"nbgrader": {
53+
"grade": false,
54+
"locked": true,
55+
"solution": false
56+
}
57+
},
58+
"source": [
59+
"## Exercise 5.1.1\n",
60+
"\n",
61+
"In the code cell bellow you can see a function that finds the greatest common divisor of any two numbers using the <code>math</code> module. "
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {
68+
"colab": {
69+
"base_uri": "https://localhost:8080/"
70+
},
71+
"collapsed": true,
72+
"id": "YjB37kkNGsr9",
73+
"outputId": "36e77aac-9e26-4c63-fbf5-e030e5539ab1"
74+
},
75+
"outputs": [],
76+
"source": [
77+
"import math\n",
78+
"\n",
79+
"def find_greatest_common_divisor(a, b):\n",
80+
" greatest_common_divisor = math.gcds(a, b)\n",
81+
" return greatest_common_divisor\n",
82+
"\n",
83+
"print('The greatest common divisor is:', find_greatest_common_divisor(2, 4))"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"<iframe src=\"https://tudelft.h5p.com/content/1292447931155380807/embed\" aria-label=\"Exercise 2.1.1\" width=\"1088\" height=\"637\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" allow=\"autoplay *; geolocation *; microphone *; camera *; midi *; encrypted-media *\"></iframe><script src=\"https://tudelft.h5p.com/js/h5p-resizer.js\" charset=\"UTF-8\"></script>"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"tags": [
98+
"remove-input"
99+
]
100+
},
101+
"outputs": [],
102+
"source": [
103+
"# jupyterquiz-exercise-2-1-1\n",
104+
"#display_quiz(questions[0])"
105+
]
106+
},
107+
{
108+
"attachments": {},
109+
"cell_type": "markdown",
110+
"metadata": {
111+
"id": "aaxayx2RH9IM",
112+
"nbgrader": {
113+
"grade": false,
114+
"locked": true,
115+
"solution": false
116+
}
117+
},
118+
"source": [
119+
"## (Searching) Exercise 5.1.2\n",
120+
"\n",
121+
"We can only take and store measurements at a limited number of locations and/or times. But what if we are interested in a value in between, i.e., at a location/time where we do not have a measurement? Then we can use interpolation to estimate that value. A popular, and simple, interpolation technique is <a href=\"https://en.wikipedia.org/wiki/Linear_interpolation\">linear interpolation</a>. \n",
122+
"Your task is to use the module <code>scipy</code> to perform a 1D linear interpolation between a set of known points, where <code>x_known</code> and <code>y_known</code> are arrays with the measured $x$ and $y$ values. Use Google to look up the 1D interpolation function in <code>scipy</code>.<br><br>In the code below there is something missing after `return` (look the three dots). What should be the correct missing code for the function to give us the desired result?"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": null,
128+
"metadata": {
129+
"collapsed": true,
130+
"id": "ZazhpxxTIzxE"
131+
},
132+
"outputs": [],
133+
"source": [
134+
"from scipy import interpolate\n",
135+
"\n",
136+
"def interpolate_inbetween(x_known, y_known, x_predict):\n",
137+
" f = interpolate.interp1d(x_known, y_known)\n",
138+
" return ...\n"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {},
144+
"source": [
145+
"<iframe src=\"https://tudelft.h5p.com/content/1292447937376291357/embed\" aria-label=\"Exercise 2.1.2\" width=\"1088\" height=\"637\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" allow=\"autoplay *; geolocation *; microphone *; camera *; midi *; encrypted-media *\"></iframe><script src=\"https://tudelft.h5p.com/js/h5p-resizer.js\" charset=\"UTF-8\"></script>"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"metadata": {
152+
"tags": [
153+
"remove-input"
154+
]
155+
},
156+
"outputs": [],
157+
"source": [
158+
"# jupyterquiz-exercise-2-1-2\n",
159+
"#display_quiz(questions[1])"
160+
]
161+
},
162+
{
163+
"attachments": {},
164+
"cell_type": "markdown",
165+
"metadata": {
166+
"id": "Ue4CKV8PKnBG",
167+
"nbgrader": {
168+
"grade": false,
169+
"locked": true,
170+
"solution": false
171+
}
172+
},
173+
"source": [
174+
"## (Searching) Exercise 5.1.3\n",
175+
"\n",
176+
"Now, let's try to measure the running time of a function, for that we will need the <code>time</code> module. Use it to measure the working time of the <code>cool_function()</code> below."
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {
183+
"collapsed": true,
184+
"id": "C1aOizjOK7sl",
185+
"nbgrader": {
186+
"grade": false,
187+
"locked": true,
188+
"solution": false
189+
}
190+
},
191+
"outputs": [],
192+
"source": [
193+
"# you do not need to change anything in this cell\n",
194+
"def cool_function():\n",
195+
" x = 0\n",
196+
" for i in range(100000000):\n",
197+
" x += 1\n"
198+
]
199+
},
200+
{
201+
"attachments": {},
202+
"cell_type": "markdown",
203+
"metadata": {},
204+
"source": [
205+
"Which of the following functions will give us the working time of the <code>cool_function()</code>?"
206+
]
207+
},
208+
{
209+
"attachments": {},
210+
"cell_type": "markdown",
211+
"metadata": {},
212+
"source": [
213+
"\n",
214+
"<div style=\"padding: 10px; border: 1px solid gray;\">\n",
215+
"\n",
216+
"* **Option A**\n",
217+
"```python\n",
218+
"def measure_time(func):\n",
219+
" t0 = time.time()\n",
220+
" t1 = time.time()\n",
221+
" return t1 - t0\n",
222+
"```\n",
223+
"\n",
224+
"* **Option B**\n",
225+
"```python\n",
226+
"def measure_time(func):\n",
227+
" t0 = time.time()\n",
228+
" func()\n",
229+
" t1 = time.time()\n",
230+
" return t1 - t0\n",
231+
"```\n",
232+
"* **Option C**\n",
233+
"```python\n",
234+
"def measure_time(func,t0,t1):\n",
235+
" t0 = time.time()\n",
236+
" func()\n",
237+
" t1 = time.time()\n",
238+
" return func\n",
239+
"```\n",
240+
"\n",
241+
"</div>\n"
242+
]
243+
},
244+
{
245+
"cell_type": "markdown",
246+
"metadata": {},
247+
"source": [
248+
"<iframe src=\"https://tudelft.h5p.com/content/1292447953779786487/embed\" aria-label=\"Exercise 2.1.3(needrevision)\" width=\"1088\" height=\"637\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" allow=\"autoplay *; geolocation *; microphone *; camera *; midi *; encrypted-media *\"></iframe><script src=\"https://tudelft.h5p.com/js/h5p-resizer.js\" charset=\"UTF-8\"></script>"
249+
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"metadata": {
255+
"tags": [
256+
"remove-input"
257+
]
258+
},
259+
"outputs": [],
260+
"source": [
261+
"# jupyterquiz-exercise-2-1-3\n",
262+
"#display_quiz(questions[2])"
263+
]
264+
}
265+
],
266+
"metadata": {
267+
"colab": {
268+
"collapsed_sections": [],
269+
"name": "Python_2_1.ipynb",
270+
"provenance": []
271+
},
272+
"kernelspec": {
273+
"display_name": "mude",
274+
"language": "python",
275+
"name": "python3"
276+
},
277+
"language_info": {
278+
"codemirror_mode": {
279+
"name": "ipython",
280+
"version": 3
281+
},
282+
"file_extension": ".py",
283+
"mimetype": "text/x-python",
284+
"name": "python",
285+
"nbconvert_exporter": "python",
286+
"pygments_lexer": "ipython3",
287+
"version": "3.11.9"
288+
}
289+
},
290+
"nbformat": 4,
291+
"nbformat_minor": 1
292+
}

0 commit comments

Comments
 (0)