This is the solution to Homework 4: Problems - Python variables and assignment statements.

The following figure illustrates the grade distribution for this homework.

Maximum possible points is 100.



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.

Answer:
A version of the code can be downloaded from here.





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.

Answer:
The corrected file can be downloaded from 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.)

Answer:
A version of the code can be downloaded from here.





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

Answer:

(A)

# Assign a value to a new variable
a = 5
 
# Create an alias identifier for this variable
b = a
 
# Observe how they refer to the same variable!
print (id(a), id(b))
 
# Create another alias
c = b
 
# Now assign a new value to b!
b = 3
 
# And observe how a and c are still the same variable
# But b is not
print (a,b,c)
print (id(a),id(b),id(c))
 
# Now for another quirk, suppose we do this:
b = a
b = 5
 
# We used an assignment, but the value didn't actually change
# So the alias remains unbroken
print (id(a), id(b))


(B)

# Create a new <list>
a = [5]
 
# Create an alias identifier for this list
b = a
print (id(a), id(b))
 
# Now change the <list> b in-place
b.append(1)
 
# And observe how this also changes a
# The alias is not broken by in-place operations
print (a,b)
print (id(a),id(b))


(C)

# Create a <list>
a = [5]
 
# Create a new <list> with the same value
b = list(a)
 
# We now have two separate variables with identical but separate values
print (a,b)
print (id(a), id(b))
 
# Same with the full slice technique:
b = a[:]
print (a,b)
print (id(a), id(b))


(D)

# Create <tuple>
a = (5,)
 
# Try to force a copy
b = tuple(a)
 
# It didn't work...
print (id(a), id(b))
 
# Neither does this
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)

Answer:

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

Later on, we will learn more effective methods of constructing this dictionary using Python’s for-loops.



Comments