Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 1

The first exercise concerns some very basic mathematics. Write a Python program that stores the result of the computation \(1+1\) in a variable and then prints the value of this variable. Name of program file: 1 plus1.py.

Problem 2

Almost all books about programming languages start with a very simple program that prints the text "Hello, World!" to the screen. Make such a program in Python. Name of program file: hello_world.py.

Problem 4

Make a program where you set a length given in meters and then compute and write out the corresponding length measured in inches, in feet, in yards, and in miles. Use that one inch is \(2.54 \mathrm{~cm}\), one foot is 12 inches, one yard is 3 feet, and one British mile is 1760 yards. As a verification, a length of 640 meters corresponds to \(25196.85\) inches, \(2099.74\) feet, \(699.91\) yards, or \(0.3977\) miles. Name of program file: length_conversion.py.

Problem 6

Let \(p\) be a bank's interest rate in percent per year. An initial amount \(A\) has then grown to $$ A\left(1+\frac{p}{100}\right)^{n} $$ after \(n\) years. Make a program for computing how much money 1000 euros have grown to after three years with \(5 \%\) interest rate. Name of program file: interest_rate.py.

Problem 10

The bell-shaped Gaussian function, $$ f(x)=\frac{1}{\sqrt{2 \pi} s} \exp \left[-\frac{1}{2}\left(\frac{x-m}{s}\right)^{2}\right] $$ is one of the most widely used functions in science and technology \(^{32}\). The parameters \(m\) and \(s\) are real numbers, where \(s\) must be greater than zero. Make a program for evaluating this function when \(m=0, s=2\), and \(x=1\). Verify the program's result by comparing with hand calculations on a calculator. Name of program file: Gaussian_function1.py.

Problem 11

The drag force, due to air resistance, on an object can be expressed as $$ F_{d}=\frac{1}{2} C_{D} \varrho A V^{2} $$ where \(\varrho\) is the density of the air, \(V\) is the velocity of the object, \(A\) is the cross-sectional area (normal to the velocity direction), and \(C_{D}\) is the drag coefficient, which depends heavily on the shape of the object and the roughness of the surface. The gravity force on an object with mass \(m\) is \(F_{g}=m g\), where \(g=9.81 \mathrm{~ms}^{-2}\). We can use the formulas for \(F_{d}\) and \(F_{g}\) to study the importance of air resistance versus gravity when kicking a football. The density of air is \(\varrho=1.2 \mathrm{~kg} \mathrm{~m}^{-3}\). We have \(A=\pi a^{2}\) for any ball with radius \(a\). For a football \(a=11 \mathrm{~cm}\). The mass of a football is \(0.43 \mathrm{~kg}, C_{D}\) can be taken as \(0.2\). Make a program that computes the drag force and the gravity force on a football. Write out the forces with one decimal in units of Newton \(\left(\mathrm{N}=\mathrm{kg} \mathrm{m} / \mathrm{s}^{2}\right)\). Also print the ratio of the drag force and the gravity force. Define \(C_{D}, \varrho, A, V, m, g, F_{d}\), and \(F_{g}\) as variables, and put a comment with the corresponding unit. Use the program to calculate the forces on the ball for a hard kick, \(V=120 \mathrm{~km} / \mathrm{h}\) and for a soft kick, \(V=10 \mathrm{~km} / \mathrm{h}\) (it is easy to mix inconsistent units, so make sure you compute with \(V\) expressed in \(\mathrm{m} / \mathrm{s}\) ). Name of program file: kick.py.

Problem 12

Start ipython and give the following command, which will save the interactive session to a file mysession.log: $$ \text { In [1]: \%logstart }-r \text {-o mysession.log } $$ Thereafter, define an integer, a real number, and a string in IPython. Apply the type function to check that each object has the right type. Print the three objects using printf syntax. Finally, type logoff to end the recording of the interactive session: $$ \text { In [8]: \%logoff } $$ Leave IPython and restart it as ipython -logplay mysession.log on the command line. IPython will now re-execute the input statements in the logfile mysession.log so that you get back the variables you declared. Print out the variables to demonstrate this fact.

Problem 13

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 \mathrm{C}\), while in the yolk the proteins start to coagulate for temperatures above \(70 \mathrm{C}\). For a soft boiled egg, the white needs to have been heated long enough to coagulate at a temperature above \(63 \mathrm{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 \mathrm{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 \mathrm{~g}\) for a small egg and \(M=67 \mathrm{~g}\) for a large egg, \(\rho=1.038 \mathrm{~g} \mathrm{~cm}^{-3}, c=3.7 \mathrm{Jg}^{-1} \mathrm{~K}^{-1}\), and \(K=5.4 \cdot 10^{-3} \mathrm{Wcm}^{-1} \mathrm{~K}^{-1}\). Furthermore, \(T_{w}\) is the temperature (in \(\mathrm{C}\) degrees) of the boiling water, and \(T_{o}\) is the original temperature (in \(\mathrm{C}\) degrees) of the egg before being put in the water. Implement the formula in a program, set \(T_{w}=100 \mathrm{C}\) and \(T_{y}=70 \mathrm{C}\), and compute \(t\) for a large egg taken from the fridge \(\left(T_{o}=4 \mathrm{C}\right)\) and from room temperature \(\left(T_{o}=20 \mathrm{C}\right)\). Name of program file: egg.py.

Problem 15

Some versions of our program for calculating the formula (1.2) are listed below. Determine which versions that will not work correctly and explain why in each case. $$ \begin{array}{llll} \mathrm{C}=21 ; & \mathrm{F}=9 / 5 * \mathrm{C}+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21.0 ; & \mathrm{F}=(9 / 5) * \mathrm{C}+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21.0 ; & \mathrm{F}=9 * \mathrm{C} / 5+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21.0 ; & \mathrm{F}=9 . *(\mathrm{C} / 5 \cdot 0)+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21.0 ; & \mathrm{F}=9.0 * \mathrm{C} / 5 \cdot 0+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21 ; & \mathrm{F}=9 * \mathrm{C} / 5+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21.0 ; & \mathrm{F}=(1 / 5) * 9 * \mathrm{C}+32 ; & & \text { print } \mathrm{F} \\ \mathrm{C}=21 ; & \mathrm{F}=(1 . / 5) * 9 * \mathrm{C}+32 ; & & \text { print } \mathrm{F} \end{array} $$

Problem 16

Find out why the following program does not work: $$ \begin{aligned} &C=A+B \\ &A=3 \\ &B=2 \\ &\text { print } C \end{aligned} $$

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks