Skip to content

Commit d1d00d0

Browse files
committed
Tidied up revision lecture
1 parent c89058b commit d1d00d0

File tree

1 file changed

+94
-19
lines changed

1 file changed

+94
-19
lines changed

notebook/Revision-1-Introduction-to-programming-for-geoscientists-Exercises.ipynb

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"metadata": {
3-
"name": "Revision-1-Introduction-to-programming-for-geoscientists-Exercises"
3+
"name": "",
4+
"signature": "sha256:120c22cc5f5f1abb596b7366aadef3837c4305d74dca5e36bc3c6168e131fea5"
45
},
56
"nbformat": 3,
67
"nbformat_minor": 0,
@@ -26,10 +27,10 @@
2627
"cell_type": "markdown",
2728
"metadata": {},
2829
"source": [
29-
"* **Find errors in the coding of formulas**</br>\n",
30+
"**1. Find errors in the coding of formulas**</br>\n",
3031
"Some versions of the program for calculating the formula\n",
3132
"$$F = \\frac{9}{5}C + 32$$\n",
32-
"are listed below. Determine which versions will not work correctly and explain why in each case."
33+
"are listed below. Add comments to each line indicating whether the expression will work correctly. If it does not work correctly then also explain why."
3334
]
3435
},
3536
{
@@ -53,7 +54,7 @@
5354
"cell_type": "markdown",
5455
"metadata": {},
5556
"source": [
56-
"* **Index a nested lists**</br>\n",
57+
"**2. Index a nested lists**</br>\n",
5758
"We define the following nested list:"
5859
]
5960
},
@@ -71,14 +72,39 @@
7172
"cell_type": "markdown",
7273
"metadata": {},
7374
"source": [
74-
"Index this list to extract 1) the letter *a*; 2) the list *['d', 'e', 'f']*; 3) the last element *h*; 4) the *d* element. Explain why *q[-1][-2]* has the value *g*."
75+
"Use the approperiate list index to extract and print:\n",
76+
"\n",
77+
"* the letter *a*,\n",
78+
"* the list *['d', 'e', 'f']*,\n",
79+
"* the last element *h*,\n",
80+
"* the *d* element."
7581
]
7682
},
83+
{
84+
"cell_type": "code",
85+
"collapsed": false,
86+
"input": [],
87+
"language": "python",
88+
"metadata": {},
89+
"outputs": []
90+
},
7791
{
7892
"cell_type": "markdown",
7993
"metadata": {},
8094
"source": [
81-
"* **Implement the sum function**</br>\n",
95+
"Explain why *q[-1][-2]* has the value *g*."
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": []
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"**3. Implement the sum function**</br>\n",
82108
"The standard Python function called *sum* takes a list as argument and computes the sum of the elements in the list:"
83109
]
84110
},
@@ -92,6 +118,7 @@
92118
"metadata": {},
93119
"outputs": [
94120
{
121+
"metadata": {},
95122
"output_type": "pyout",
96123
"prompt_number": 1,
97124
"text": [
@@ -108,31 +135,35 @@
108135
"Implement your own version of *sum*."
109136
]
110137
},
138+
{
139+
"cell_type": "code",
140+
"collapsed": false,
141+
"input": [],
142+
"language": "python",
143+
"metadata": {},
144+
"outputs": []
145+
},
111146
{
112147
"cell_type": "markdown",
113148
"metadata": {},
114149
"source": [
115-
"* **Construct a double for loop over a nested list**</br>\n",
150+
"**4. Construct a double for loop over a nested list**</br>\n",
116151
"Consider the list *q* defined earlier. Construct a double *for* loop (i.e. a loop within a loop) to print out all of the elements in the nested list."
117152
]
118153
},
119154
{
120-
"cell_type": "markdown",
155+
"cell_type": "code",
156+
"collapsed": false,
157+
"input": [],
158+
"language": "python",
121159
"metadata": {},
122-
"source": [
123-
"* **How to cook the perfect egg**</br>\n",
124-
"As an egg cooks, the proteins first denature and then coagulate. When the temperature exceeds a critical point, reactions begin and proceed faster as the temperature increases. In the egg white the proteins start to coagulate for temperatures above 63 C, while in the yolk the proteins start to coagulate for temperatures above 70 C. For a soft boiled egg, the white needs to have been heated long enough to coagulate at a temperature above 63 C, but the yolk should not be heated above 70 C. For a hard boiled egg, the center of the yolk should be allowed to reach 70 C. The following formula expresses the time $t$ it takes (in seconds) for the center of the yolk to reach the temperature $T_y$ (in Celsius degrees):\n",
125-
"\n",
126-
"$$t = \\frac{M^{2/3}c\\rho^{1/3}}{K\\pi^2(4\\pi/3)^{2/3}}\\ln\\left[0.76\\frac{T_o - T_w}{T_y - T_w}\\right]$$\n",
127-
"\n",
128-
"Here, $M, \\rho, c$ and $K$ are properties of the egg: $M$ is the mass, $\\rho$ is the density, $c$ is the specific heat capacity, and $K$ is thermal conductivity. Relevant values are $M$ = 47 g for a small egg and $M$ = 67 g for a large egg, $\\rho$ = 1.038 g cm$^{-3}$, $c$ = 3.7 J g$^{-1}$K$^{-1}$, and $K$ = 5.4 $\\times$ 10$^{-3}$ W cm$^{-1}$ K$^{-1}$. Furthermore, $T_w$ is the temperature (in C degrees) of the boiling water, and $T_o$ is the original temperature (in C degrees) of the egg before being put in the water. Implement the formula in a program, set $T_w$ = 100 C and $T_y$ = 70 C, and compute $t$ for a large egg taken from the fridge ($T_o$ = 4 C) and from room temperature ($T_o$ = 20 C). \n"
129-
]
160+
"outputs": []
130161
},
131162
{
132163
"cell_type": "markdown",
133164
"metadata": {},
134165
"source": [
135-
"* **Implement the factorial function**</br>\n",
166+
"**5. Implement the factorial function**</br>\n",
136167
"The factorial of $n$, written as $n!$, is defined as\n",
137168
"\n",
138169
"$$n! = n(n \u2212 1)(n \u2212 2) \\cdots 2 \\cdot 1,$$\n",
@@ -144,26 +175,70 @@
144175
"For example, $4! = 4 \\cdot 3 \\cdot 2 \\cdot 1 = 24$, and $2! = 2 \\cdot 1 = 2$. Write a function *fact(n)* that returns $n!$. Return 1 immediately if $x$ is 1 or 0, otherwise use a loop to compute $n!$."
145176
]
146177
},
178+
{
179+
"cell_type": "code",
180+
"collapsed": false,
181+
"input": [],
182+
"language": "python",
183+
"metadata": {},
184+
"outputs": []
185+
},
147186
{
148187
"cell_type": "markdown",
149188
"metadata": {},
150189
"source": [
151-
"* **Add error checking**</br>\n",
190+
"**6. Add error checking**</br>\n",
152191
"Add in a try-except block to the function *fact(n)*. An exception should be thrown/raised if the number *n* is negative. If an exception occurs, your program should print a helpful error message and quit."
153192
]
154193
},
194+
{
195+
"cell_type": "code",
196+
"collapsed": false,
197+
"input": [],
198+
"language": "python",
199+
"metadata": {},
200+
"outputs": []
201+
},
155202
{
156203
"cell_type": "markdown",
157204
"metadata": {},
158205
"source": [
159-
"* **Compute the length of a path**</br>\n",
206+
"**7. Compute the length of a path**</br>\n",
160207
"Some object is moving along a path in the plane. At $n$ points of time we have recorded the corresponding $(x, y)$ positions of the object:\n",
161208
"$(x_0, y_0), (x_1, y_1), \\ldots, (x_{n-1}, y_{n-1})$. The total length $L$ of the path from $(x_0, y_0)$ to $(x_{n-1}, y_{n-1})$ is the sum of all the individual line segments $(x_{i-1}, y_{i-1})$ to $(x_i, y_i)$, $i = 1, \\ldots, n-1$:\n",
162209
"\n",
163210
"$$L = \\sum_{i=1}^{n-1}{\\sqrt{(x_i - x_{i-1})^2 + (y_i - y_{i-1})^2}}.$$\n",
164211
"\n",
165212
"Make a function *pathlength(x, y)* for computing $L$ according to the formula. The arguments $x$ and $y$ hold all the $x_0, \\ldots, x_{n-1}$ and $y_0, \\ldots, y_{n-1}$ coordinates, respectively. Test the function on a triangular path with the four points (1, 1), (2, 1), (1, 2), and (1, 1)."
166213
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"collapsed": false,
218+
"input": [],
219+
"language": "python",
220+
"metadata": {},
221+
"outputs": []
222+
},
223+
{
224+
"cell_type": "markdown",
225+
"metadata": {},
226+
"source": [
227+
"**8. How to cook the perfect egg**</br>\n",
228+
"As an egg cooks, the proteins first denature and then coagulate. When the temperature exceeds a critical point, reactions begin and proceed faster as the temperature increases. In the egg white the proteins start to coagulate for temperatures above 63 C, while in the yolk the proteins start to coagulate for temperatures above 70 C. For a soft boiled egg, the white needs to have been heated long enough to coagulate at a temperature above 63 C, but the yolk should not be heated above 70 C. For a hard boiled egg, the center of the yolk should be allowed to reach 70 C. The following formula expresses the time $t$ it takes (in seconds) for the center of the yolk to reach the temperature $T_y$ (in Celsius degrees):\n",
229+
"\n",
230+
"$$t = \\frac{M^{2/3}c\\rho^{1/3}}{K\\pi^2(4\\pi/3)^{2/3}}\\ln\\left[0.76\\frac{T_o - T_w}{T_y - T_w}\\right]$$\n",
231+
"\n",
232+
"Here, $M, \\rho, c$ and $K$ are properties of the egg: $M$ is the mass, $\\rho$ is the density, $c$ is the specific heat capacity, and $K$ is thermal conductivity. Relevant values are $M$ = 47 g for a small egg and $M$ = 67 g for a large egg, $\\rho$ = 1.038 g cm$^{-3}$, $c$ = 3.7 J g$^{-1}$K$^{-1}$, and $K$ = 5.4 $\\times$ 10$^{-3}$ W cm$^{-1}$ K$^{-1}$. Furthermore, $T_w$ is the temperature (in C degrees) of the boiling water, and $T_o$ is the original temperature (in C degrees) of the egg before being put in the water. Implement the formula in a program, set $T_w$ = 100 C and $T_y$ = 70 C, and compute $t$ for a large egg taken from the fridge ($T_o$ = 4 C) and from room temperature ($T_o$ = 20 C). \n"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"collapsed": false,
238+
"input": [],
239+
"language": "python",
240+
"metadata": {},
241+
"outputs": []
167242
}
168243
],
169244
"metadata": {}

0 commit comments

Comments
 (0)