Starting from:

$30

Project 2 (Programs with Control Flow)

Project 2 (Programs with Control Flow)
Exercises
Exercise 1. (Quadratic Equation) Write a program called quadratic.py (a variant of the program we discussed in class) that
accepts a (float), b (float), and c (float) as command-line arguments, and writes to standard output the roots of the quadratic
equation ax2 + bx + c = 0. Your program should report the message “Value of a must not be 0” if a = 0, and the message
“Value of discriminant must not be negative” if b
2 − 4ac < 0.
& ~/workspace/project2
$ python3 quadratic . py 1 -5 6
3.0 2.0
$ python3 quadratic . py 1 -1 -1
1.618033988749895 -0.6180339887498949
Exercise 2. (Six-sided Die) Write a program called die.py that simulates the roll of a six-sided die, and writes to standard
output the pattern on the top face.
& ~/workspace/project2
$ python3 die . py
* *
*
* *
$ python3 die . py
*
*
Exercise 3. (Primality Test) Write a program called primality_test.py that accepts n (int) as command-line argument, and
writes to standard output if n is a prime number or not.
& ~/workspace/project2
$ python3 primality_test . py 31
True
$ python3 primality_test . py 42
False
Exercise 4. Write a program called factorial.py that accepts n (int) as command-line argument, and writes to standard
output the value of n!, which is defined as n! = 1 × 2 × . . .(n − 1) × n. Note that 0! = 1.
& ~/workspace/project2
$ python3 factorial . py 0
1
$ python3 factorial . py 5
120
Exercise 5. (Counting Primes) Write a program called prime_counter.py that accepts n (int) as command-line argument, and
writes to standard output the number of primes less than or equal to n.
& ~/workspace/project2
$ python3 prime_counter . py 10
4
$ python3 prime_counter . py 100
25
1 / 6
Project 2 (Programs with Control Flow)
Problems
Problem 1. (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. Your program should report the message “Value of t must be ≤ 50 F” if t > 50, and the message
“Value of v must be > 3 mph” if v ≤ 3.
& ~/workspace/project2
$ python3 wind_chill . py 55 15
Value of t must be <= 50 F
$ python3 wind_chill . py 32 15
21.588988890532022
Directions:
ˆ Use an if statement to decide when to report the error messages and when to compute and write the wind chill w.
Problem 2. (Day of the Week) Write a program called day_of_week.py that accepts m (int), d (int), and y (int) as command-line
arguments, computes the day of the week (0 for Sunday, 1 for Monday, and so on) dow using the formulae below, and writes
the day as a string (“Sunday”, “Monday”, and so on) to standard output.
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/project2
$ python3 day_of_week . py 3 14 1879
Friday
$ python3 day_of_week . py 4 12 1882
Wednesday
Directions:
ˆ After computing dow, use an if statement to write the correct output based on the value of dow.
Problem 3. (Playing Card) Write a program called card.py that simulates the selection of a random card from a standard
deck of 52 playing cards, and writes it to standard output.
& ~/workspace/project2
$ python3 card . py
3 of Clubs
$ python3 card . py
Ace of Spades
Directions:
ˆ Set rank to a random integer from [2, 14].
ˆ Use an if statement to set rankStr to a string corresponding to rank — the ranks are 2, 3, . . . , Jack, Queen, King,
and Ace.
2 / 6
Project 2 (Programs with Control Flow)
ˆ Set suit to a random integer from [1, 4].
ˆ Use an if statement to set suitStr to a string corresponding to suit — the suits are Clubs, Diamonds, Hearts, and
Spades.
ˆ Write the desired output.
Problem 4. (Root Finding) Write a program called root.py (a variant of the sqrt.py program we dicussed in class) that accepts
k (int), c (float), and epsilon (float) as command-line arguments, and writes to standard output the kth root of c, up to
epsilon decimal places.
& ~/workspace/project2
$ python3 root . py 3 2 1e -15
1.2599210498948732
$ python3 root . py 3 27 1e -15
3.0
Directions:
ˆ Set t (our guess) to c.
ˆ Repeat as long as |1 − c/tk
| > :
– Replace t by t − f(t)/f0
(t), where f(t) = t
k − c and f
0
(t) = ktk−1
.
ˆ Write t (the kth root of c).
Problem 5. (Greatest Common Divisor ) Write a program called gcd.py that accepts p (int) and q (int) as command-line
arguments, and writes to standard output the greatest common divisor (gcd) of p and q.
& ~/workspace/project2
$ python3 gcd . py 408 1440
24
$ python3 gcd . py 21 22
1
Directions:
ˆ Repeat as long as p mod q 6= 0:
– Exchange p and q with q and p mod q.
ˆ Write q (the gcd).
Problem 6. (Fibonacci Number ) Write a program called fibonacci.py that accepts n (int) as command-line argument, and
writes to standard output the nth number from the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, . . .).
& ~/workspace/project2
$ python3 fibonacci . py 10
55
$ python3 fibonacci . py 15
610
Directions:
ˆ Set a, b (the first two Fibonacci numbers) to 1, and i to 3.
ˆ Repeat as long as i ≤ n:
3 / 6
Project 2 (Programs with Control Flow)
– Exchange a and b with b and a + b.
– Increment i by 1.
ˆ Write b (nth Fibonacci number).
Problem 7. (Sum of Powers) Write a program called sum_of_powers.py that accepts n (int) and k (int) as command-line
arguments, and writes to standard output the sum 1k + 2k + · · · + n
k
.
& ~/workspace/project2
$ python3 sum_of_powers . py 15 1
120
$ python3 sum_of_powers . py 10 3
3025
Directions:
ˆ Set total to 0.
ˆ Repeat for each i ∈ [1, n]:
– Increment total by i
k
.
ˆ Write total (sum of powers).
Problem 8. (Dragon Curve) The instructions for drawing a dragon curve are strings of the characters F, L, and R, where
F means “draw a line while moving 1 unit forward”, L means “turn left”, and R means “turn right”. The key to solving
this problem is to note that a curve of order n is a curve of order n − 1 followed by an L followed by a curve of order n − 1
traversed in reverse order, replacing L with R and R with L. Write a program called dragon_curve.py that accepts n (int) as
command-line argument, and writes to standard output the instructions for drawing a dragon curve of order n.
& ~/workspace/project2
$ python3 dragon_curve . py 0
F
$ python3 dragon_curve . py 1
FLF
$ python3 dragon_curve . py 2
FLFLFRF
$ python3 dragon_curve . py 3
FLFLFRFLFLFRFRF
Directions:
ˆ Set dragon and nogard to the string “F”.
ˆ Repeat for each i ∈ [1, n]:
– Exchange dragon and nogard with dragon “L” nogard and dragon “R” nogard.
ˆ Write dragon (dragon curve of order n).
Problem 9. (Perfect Numbers) A perfect number is a positive integer whose proper divisors add up to the number. For
example, 6 is a perfect number since its proper divisors 1, 2, and 3 add up to 6. Write a program called perfect_numbers.py that
accepts n (int) as command-line argument, and writes to standard output the perfect numbers that are less than or equal to
n.
4 / 6
Project 2 (Programs with Control Flow)
& ~/workspace/project2
$ python3 perfect_numbers . py 10
6
$ python3 perfect_numbers . py 1000
6
28
496
Directions:
ˆ Repeat for each i ∈ [2, n]:
– Set total (sum of divisors of i) to 0.
– Repeat for each j ∈ [1, i/2]:
* If i mod j = 0, increment total by j.
– If total = i, write i (perfect number).
Problem 10. (Ramanujan Numbers) Srinivasa Ramanujan was an Indian mathematician who became famous for his intuition
for numbers. When the English mathematician G. H. Hardy came to visit him one day, Hardy remarked that the number of
his taxi was 1729, a rather dull number. Ramanujan replied, “No, Hardy! It is a very interesting number. It is the smallest
number expressible as the sum of two cubes in two different ways.” Verify this claim by writing a program ramanujan_numbers.py
that accepts n (int) as command-line argument, and writes to standard output all integers less than or equal to n that can
be expressed as the sum of two cubes in two different ways. In other words, find distinct positive integers a, b, c, and d such
that a
3 + b
3 = c
3 + d
3 ≤ n.
& ~/workspace/project2
$ python3 ramanujan_numbers . py 10000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
$ python3 ramanujan_numbers . py 40000
1729 = 1^3 + 12^3 = 9^3 + 10^3
4104 = 2^3 + 16^3 = 9^3 + 15^3
13832 = 2^3 + 24^3 = 18^3 + 20^3
39312 = 2^3 + 34^3 = 15^3 + 33^3
32832 = 4^3 + 32^3 = 18^3 + 30^3
20683 = 10^3 + 27^3 = 19^3 + 24^3
Directions:
ˆ Use four nested while loops, with the following bounds on the loop variables:
1 ≤ a ≤
√3 n
a + 1 ≤ b ≤
p3
n − a
3
a + 1 ≤ c ≤
√3 n
c + 1 ≤ d ≤
p3
n − c
3
ˆ Inside the innermost loop, if a
3 + b
3 = c
3 + d
3
, write the desired output.
Note: use x × x × x instead of x
3 and x × x × x ≤ y in place of x ≤
√3 y.
Files to Submit
1. quadratic.py
2. die.py
3. primality_test.py
5 / 6
Project 2 (Programs with Control Flow)
4. factorial.py
5. prime_counter.py
6. wind_chill.py
7. day_of_week.py
8. card.py
9. root.py
10. gcd.py
11. fibonacci.py
12. sum_of_powers.py
13. dragon_curve.py
14. perfect_numbers.py
15. ramanujan_numbers.py
16. report.txt
Before you submit your files, make sure:
ˆ You do not use concepts from sections beyond “Control Flow”.
ˆ Your programs meet the style requirements by running the following command in the terminal.
& ~/workspace/project2
$ 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

More products