1. Find errors in the coding of formulas Some versions of the program for calculating the formula $$F = \frac{9}{5}C + 32$$ 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.
C = 21; F = 9/5*C + 32; print F
C = 21.0; F = (9/5)*C + 32; print F
C = 21.0; F = 9*C/5 + 32; print F
C = 21.0; F = 9.*(C/5.0) + 32; print F
C = 21.0; F = 9.0*C/5.0 + 32; print F
C = 21; F = 9*C/5 + 32; print F
C = 21.0; F = (1/5)*9*C + 32; print F
C = 21; F = (1./5)*9*C + 32; print F
2. Index a nested lists We define the following nested list:
q = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
Use the approperiate list index to extract and print:
Explain why q[-1][-2] has the value g.
3. Implement the sum function The standard Python function called sum takes a list as argument and computes the sum of the elements in the list:
sum([1,3,5,-5])
4
Implement your own version of sum.
4. Construct a double for loop over a nested list 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.
5. Implement the factorial function The factorial of $n$, written as $n!$, is defined as
$$n! = n(n − 1)(n − 2) \cdots 2 \cdot 1,$$with the special cases
$$1! = 1,$$$$0! = 1.$$
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!$.
6. Add error checking 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.
7. Compute the length of a path 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: $(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$:
$$L = \sum_{i=1}^{n-1}{\sqrt{(x_i - x_{i-1})^2 + (y_i - y_{i-1})^2}}.$$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).
8. How to cook the perfect egg 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):
$$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]$$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).