$30
Project 1 (Straight-line Programs)
Exercises
Exercise 1. (Name and Age) Write a program called name_age.py that accepts name (str) and age (str) as command-line
arguments, and writes “name is age years old.” to standard output.
& ~/workspace/project1
$ python3 name_age . py Alice 19
Alice is 19 years old .
$ python3 name_age . py Bob 23
Bob is 23 years old .
Exercise 2. (Greet Three) Write a program called greet_three.py that accepts name1 (str), name2 (str), and name3 as
command-line arguments, and writes “Hi name3, name2, and name1.” to standard output.
& ~/workspace/project1
$ python3 greet_three . py Alice Bob Carol
Hi Carol , Bob , and Alice .
$ python3 greet_three . py Dan Eve Fred
Hi Fred , Eve , and Dan .
Exercise 3. (Triangle Inequality) Write a program called triangle.py that accepts x (int), y (int), and z (int) as command-line
arguments, and writes to standard output True if each one of them is less than or equal to the sum of the other two, and False
otherwise.
& ~/workspace/project1
$ python3 triangle . py 3 3 3
True
$ python3 triangle . py 2 4 7
False
Exercise 4. (Body Mass Index ) The body mass index (BMI) is the ratio of the weight w of a person (in kg) to the square
of the height h (in m). Write a program called bmi.py that accepts w (float) and h (float) as command-line arguments, and
writes the BMI to standard output.
& ~/workspace/project1
$ python3 bmi . py 75 1.83
22.395413419331717
$ python3 bmi . py 97 1.75
31.6734693877551
Exercise 5. (Random Integer ) Write a program called random_int.py that accepts a (int) and b (int) as command-line arguments,
and writes to standard output a random integer between a (inclusive) and b (exclusive).
& ~/workspace/project1
$ python3 random_int . py 10 20
13
$ python3 random_int . py 10 20
10
1 / 6
Project 1 (Straight-line Programs)
Problems
Problem 1. (Day of the Week) Write a program called day_of_week.py that accepts m (int), d (int), and y (int) as commandline arguments, representing a date, and writes the day of the week (0 for Sunday, 1 for Monday, and so on) dow to standard
output, computed as
y0 = y − (14 − m)/12,
x0 = y0 + y0/4 − y0/100 + y0/400,
m0 = m + 12 × ((14 − m)/12) − 2,
dow = (d + x0 + 31 × m0/12) mod 7.
& ~/workspace/project1
$ python3 day_of_week . py 3 14 1879
5
$ python3 day_of_week . py 2 12 1809
0
Directions:
• Accept three integers m, d, and y as command-line arguments.
• Compute and write the value of day of the week dow.
• Use // (floored division) for / and % for mod.
Problem 2. (Mercator Projection) The Mercator projection is a conformal (angle preserving) projection that maps latitude
ϕ and longitude λ, expressed in degrees, to rectangular coordinates (x, y). It is widely used — for example, in nautical charts
and in the maps that you print from the web. The projection, with the center of map set to the prime meridian (0 degrees)
and radius of Earth set to 1, is defined by the equations
x = λ and y =
ln
1+sin ϕ
1−sin ϕ
2
.
Write a program called mercator.py that accepts latitude ϕ (float) and longitude λ (float) as command-line arguments and
writes the corresponding x and y values to standard output, separated by a space.
& ~/workspace/project1
$ python3 mercator . py 42.36 -71.06
-71.06 0.8176461519422712
$ python3 mercator . py 51.18 -1.83
-1.83 1.0431252282097918
Directions:
• Accept two floats ϕ and λ as command-line arguments.
• Compute and write the values of x and y, separated by a space.
• Use math.radians() to convert degrees to radians.
• Use math.log() for natural logarithm.
Problem 3. (Great Circle Distance) Write a program called great_circle.py that accepts x1 (float), y1 (float), x2 (float), and
y2 (float) as command-line arguments, representing the latitude and longitude in degrees of two points on Earth, and writes
to standard output the great-circle distance d (in km) between them, computed as
d = 6359.83 arccos(sin(x1) sin(x2) + cos(x1) cos(x2) cos(y1 − y2)).
2 /
Project 1 (Straight-line Programs)
& ~/workspace/project1
$ python3 great_circle . py 48.87 -2.33 37.8 -122.4
8701.387455462233
$ python3 great_circle . py 46.36 -71.06 39.90 116.41
10376.503884802196
Directions:
• Accept four floats x1, y1, x2, and y2 as command-line arguments.
• Compute and write the value of great-circle distance d.
• Use math.radians() to convert degrees to radians.
• To calculate the arccosine of a number, use math.acos().
Problem 4. (Wind Chill) Given the temperature t (in Fahrenheit) and the wind speed v (in miles per hour), the National
Weather Service defines the effective temperature (the wind chill) to be
w = 35.74 + 0.6215t + (0.4275t − 35.75)v
0.16
.
Write a program called wind_chill.py that accepts t (float) and v (float) as command-line arguments, and writes the wind chill
w to standard output.
& ~/workspace/project1
$ python3 wind_chill . py 32 15
21.588988890532022
$ python3 wind_chill . py 10 10
-3.5402167842280647
Directions:
• Accept two floats t and v as command-line arguments.
• Compute and write the value of wind chill w.
• Use ** for exponentiation, ie, use x ** y for x
y
.
Problem 5. (Gravitational Force) Write a program called gravitational_force.py that accepts m1 (float), m2 (float), and r
(float) as command-line arguments, representing the masses (in kg) of two objects and the distance (in m) between their
centers, and writes to standard output the gravitational force f (in N) acting between the objects, computed as
f = G
m1m2
r
2
,
where G = 6.674 × 10−11 (in m3 kg−1
s
−2
) is the gravitational constant.
& ~/workspace/project1
$ python3 gravitational_force . py 2 e30 6 e24 1.5 e11
3.5594666666666664 e +22
$ python3 gravitational_force . py 6 e24 7.35 e22 3.84 e8
1.9960083007812498 e +20
Directions:
• Accept three floats m1, m2, and r as command-line arguments.
• Compute and write the value of gravitational force f.
• Use scientific notation for the value of G (eg, 6.022e23 for 6.022 × 1023).
3 / 6
Project 1 (Straight-line Programs)
Problem 6. (Snell’s Law) Snell’s law states that given two mediums, the ratio of the sines of the angles (in degrees) of
incidence and refraction is equivalent to the reciprocal of the ratio of the indices of refraction of the two mediums, ie,
sin(θ1)
sin(θ2)
=
n2
n1
.
Write a program called snell.py that accepts θ1 (float), n1 (float), and n2 (float) as command-line arguments, and writes to
standard output the corresponding angle of refraction θ2 in degrees.
& ~/workspace/project1
$ python3 snell . py 58 1 1.52
33.912513998258994
$ python3 snell . py 30 1 1.2
24.624318352164074
Directions:
• Accept three floats θ1, n1, and n2 as command-line arguments.
• Compute and write the value of the angle of refraction θ2 in degrees.
• Use math.radians() and math.degrees() to convert degrees to radians and vice versa.
• To calculate the arcsine of a number, use math.asin().
Problem 7. (Gambler’s Ruin) Consider a coin-flipping game with two players where player one wins each toss with probability p, and player two wins with probability q = 1 − p. Suppose player one has n1 pennies and player two n2 pennies.
Assuming an unfair coin (ie, p 6= 1/2), the probabilities p1 and p2 that players one and two, respectively, will end penniless
are
p1 =
1 − (
p
q
)
n2
1 − (
p
q
)
n1+n2
and p2 =
1 − (
q
p
)
n1
1 − (
q
p
)
n1+n2
.
Write a program called gambler.py that accepts n1 (int), n2 (int), and p (float) as command-line arguments, and writes the
probabilities p1 and p2 to standard output, separated by a space.
& ~/workspace/project1
$ python3 gambler . py 10 100 0.51
0.6661883734200654 0.3338116265799349
$ python3 gambler . py 100 10 0.51
0.006110712510580903 0.9938892874894192
Directions:
• Accept integers n1, n2 and float p as command-line arguments.
• Compute and write the values of p1 and p2, separated by a space.
Problem 8. (Waiting Time) If λ is the average number of events per unit of time, the probability p that one has to wait
longer than time t until the next event is given by the exponential distribution
p = e
−λt
.
Write a program called waiting_time.py that accepts λ (float) and t (float) as command-line arguments, and writes the probability
p to standard output.
4 / 6
Project 1 (Straight-line Programs)
& ~/workspace/project1
$ python3 waiting_time . py 0.1 5
0.6065306597126334
$ python3 waiting_time . py 0.6 3
0.16529888822158656
Directions:
• Accept two floats λ and t as command-line arguments.
• Compute and write the value of p.
• Use math.exp(x) for e
x
.
Problem 9. (Die Roll) Write a program called die_roll.py that accepts n (int) as command-line argument, representing the
number of sides of a fair die, rolls an n-sided die twice, and writes to standard output the sum of the numbers rolled.
& ~/workspace/project1
$ python3 die_roll . py 6
12
$ python3 die_roll . py 6
10
Directions:
• Accept an integer n as command-line argument.
• Use stdrandom.uniformInt() with suitable arguments to simulate a single roll of an n-sided die.
• Write the sum of the numbers rolled.
Problem 10. (Three Sort) Write a program called three_sort.py that accepts x (int), y (int), and z (int) as command-line
arguments, and writes them to standard output in ascending order, separated by spaces. Your solution must only use min(),
max(), and basic arithmetic operations to figure out the ordering.
& ~/workspace/project1
$ python3 three_sort . py 1 3 2
1 2 3
$ python3 three_sort . py 3 2 1
1 2 3
Directions:
• Accept three integers x, y, and z as command-line arguments.
• Find the smallest value α and largest value ω using min() and max() functions.
• Find the middle value as an arithmetic combination of x, y, z, α, and ω.
• Write the numbers in ascending order, separated by a space.
5 / 6
Project 1 (Straight-line Programs)
Files to Submit
1. name_age.py
2. greet_three.py
3. triangle.py
4. bmi.py
5. random_int.py
6. day_of_week.py
7. mercator.py
8. great_circle.py
9. wind_chill.py
10. gravitational_force.py
11. snell.py
12. gambler.py
13. waiting_time.py
14. die_roll.py
15. three_sort.py
16. report.txt
Before you submit your files, make sure:
• You do not use concepts from sections beyond “Basic Data Types”.
• Your programs meet the style requirements by running the following command in the terminal.
& ~/workspace/project1
$ pycodestyle < program >
• Your code is adequately commented, follows good programming principles, and meets any specific requirements
such as corner cases and running times.
• You use the template file report.txt for your report.
• Your report meets the prescribed guidelines.
6 / 6