This homework aims at giving you some experience with variables in Python and their syntax, also some experience with using Lists and Dictionaries in Python. Write your Python scripts with the corresponding *.py file names, and add a readme.md file in HW 4 folder of your project if you need to add any additional explanation (Don’t forget to use markdown syntax highlight in your readme file, if needed).





1.  The bell-shaped Gaussian probability density function,


is one of the most widely used functions in science and technology. The parameters of the function (mu,sigma>0) are prescribed real numbers. Write a program for evaluating this function when $\mu=0$, $\sigma=2$, $x = 1$.

Hint: The value pi, and exp() , sqrt() function are not built-in Python entities. You will need to export them using the following command,

from math import pi, exp, sqrt


Verify your answer by getting the same result from Wolfram Alpha mathematical search engine.





2.  Download this code. This code is full syntax errors. Fix the errors and submit the corrected code with name script_full_of_errors_corrected.py in your hw4 folder of your project. Explain in front of each corrected Python statement, why the error occurred. On the last line of the script,

true = fox is rev in Persian


the statement runs without error. But can you explain what it does? (Hint: For this part you need to understand operation precedence. You can find more information here.





3.  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^\circ\rm{C}$, while in the yolk the proteins start to coagulate for temperatures above $70^\circ\rm{C}$. For a soft boiled egg, the white needs to have been heated long enough to coagulate at a temperature above $63^\circ\rm{C}$, but the yolk should not be heated above $70^\circ\rm{C}$. For a hard boiled egg, the center of the yolk should be allowed to reach $70^\circ\rm{C}$.

The following formula expresses the time $t$ it takes (in seconds) for the center of the yolk to reach the temperature Ty (in Celsius degrees):


where $M$ is the mass of egg, $\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\times10^{-3} ~[Wcm^{-1}K^{-1}]$. Furthermore, $T_w$ is the temperature (in C degrees) of the boiling water, and $T_0$ is the original temperature (in C degrees) of the egg before being put in the water. Implement the formula in a Python program, set $T_w = 100^\circ C$ and $T_y = 70^\circ C$, and compute $t$ for a large egg taken from the fridge ($T_0 = 4^\circ C$) and from room temperature ($T_0 = 20^\circ C$). (Hint: Note that for this problem you will need to import log, pi from math module.)





4.  Aliasing vs. Copying variables Run the following python statements and briefly explain why you get the behavior printed by the print functions.

(A)

a = 5
b = a
print (id(a), id(b))
 
c = b
b = 3
print (a,b,c)
print (id(a),id(b),id(c))
 
b = a
b = 5
print (id(a), id(b))


(B)

a = [5]
b = a
print (id(a), id(b))
 
b.append(1)
print a,b
print (id(a),id(b))


(C)

a = [5]
b = list(a)
print (a,b)
print (id(a), id(b))
 
b = a[:]
print (a,b)
print (id(a), id(b))


(D)

a = (5,)
b = tuple(a)
print (id(a), id(b))
 
b = a[:]
print (id(a), id(b))





5.  The following is the name list of all people in our ECL class.

nicholas boeker
bradley bridges
sagar dhana
travis driver
eric gagliano
christian garcia
matthew goree
lucero herrera
jake janssen
michael langford
colin lewis
mark loveland
emilio mendiola
kreshel nguyen  
russell philley
caleb phillips  
joseph robbins  
bradley smith
amir shahmoradi
vivek varier


We want to create a dictionary of members of this class with individuals’ full names serving as dictionary keys, and their roles in class as the corresponding values of the keys. The naive way of generating this dictionary would be to use one of the methods for generating dictionary discussed in Lecture 5. For example,

ecl_dict = { 'nicholas boeker'  : 'student'
           , 'bradley bridges'  : 'student'
           , 'sagar dhana'      : 'student' 
           , 'travis driver'    : 'student'
           , 'eric gagliano'    : 'student'
           , 'christian garcia' : 'student'
           , 'matthew goree'    : 'student'
           , 'lucero herrera'   : 'student'
           , 'jake janssen'     : 'student'
           , 'michael langford' : 'student'
           , 'colin lewis'      : 'student'
           , 'mark loveland'    : 'student'
           , 'emilio mendiola'  : 'student'
           , 'kreshel nguyen'   : 'student'
           , 'russell philley'  : 'student'
           , 'caleb phillips'   : 'student'
           , 'joseph robbins'   : 'student'
           , 'bradley smith'    : 'student'
           , 'vivek varier'     : 'assistant'
           , 'amir shahmoradi'  : 'instructor'
           }


However, the problem is that, except me (Amir, the instructor) and Vivek (our TA), everyone else has a student role in this class. So it would be a very painful process to type all ‘student’ values by hand manually. Now, the goal is to come up with a shortcut method that avoids the aforementioned problem. Can you find a way of creating this dictionary, without having to type the value ‘student’ 18 times? Note that in order to achieve this, you don’t need anything beyond what you learned in Lecture 5. (Hint: Have a look at the section for simultaneous assignments in Lecture 5.)



Comments