Place the files in a lab3 folder. Before leaving lab, hand the piece of paper in to your CA, zip up the lab3 folder, and hand the zip file in.
Doomsday Algorithm
In the Gregorian calendar (the one we use today), an amazing property exists. On any given year, the dates Apr 4 (i.e. 4/4), June 6 (6/6), August 8 ( 8/8), October 10 (10/10) and December 12 (12/12) all occur on the same day of the week. Also, so do May 9 (5/9) and September 5 (9/5) as well as July 11 (7/11) and November 7 (11/7). In addition, so does the last day of February. Also January 3 (1/3) on non leap-years and January 4 (1/4) on leap years. These days are known as "doomsdays".
To compute the day of the week of the doomsdays for the input year, follow this algorithm (good for years 2000-2099):
e | return |
0 | "Sunday" |
1 | "Monday" |
2 | "Tuesday" |
3 | "Wednesday" |
4 | "Thursday" |
5 | "Friday" |
6 | "Saturday" |
otherwise | "unknown" |
Trace this algorithm on paper for 2014. What day do you get? Check a calendar online to verify that you got the correct answer.
Write a Python function compute_day(year) in the file doomsday.py that returns the day of the week of the doomsdays for the given year using the algorithm above. Test it using python3 for at least 5 different years to see if it's working as you intended. (Is this enough testing in your opinion?)
Program Logic
Recall that a variable can store different kinds of data. A variable can hold a Boolean (logical) value of True or False.
If we connect two relational expressions (that evaluate to Boolean values) with the operator and, the result is True if both relational expressions are true; otherwise the result is False. If we connect two relational expressions with the operator or, the result is True if at least one of the relational expressions is true; otherwise the result is False.
Store the following Python function in the file legal.py that prints out what is legal to do for a person in the U.S. given the person's age, gender, and citizenship based on a simplification of US law:
def legal_actions(age, male, citizen_of_USA): print("Legal to:") if age >= 21: print("Drink alcohol.") if age >= 18 and citizen_of_USA == True: print("Vote.") if male == True: print("Get drafted into the armed forces.")
Load this program into python3 and call the function as follows:
legal_actions(25, True, True)
(You are seeing what a 25 year old male citizen of the USA can do legally of the three options in the function.) You should get all three actions printed out.
On the same paper you used for problem 1, draw a flowchart that shows the flow of control through the various conditions in the function.
On the same paper you used for problem 1, create a table as shown below, and fill in values for age, male and citizenship that will result in the given output. Use your function in python3 to test your answers. If a particular parameter does not matter for a specific output, indicate this in the table as "anything". If a particular output is impossible, then indicate "None" in each of the age, male and citizenship columns.
Output | Age | Male? | US Citizen? |
---|---|---|---|
DINK, VOTE & DRAFTED | 25 | True | True |
DRINK & VOTE only | |||
DRINK & DRAFTED only | |||
VOTE & DRAFTED only | |||
DRINK only | |||
VOTE only | |||
DRAFTED only | |||
No output |
EXTRA: Think about how your results change if the operator and in the function above were replaced with or.
Nested Loops
Consider the following output as shown:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Store the following Python function in the file triangular.py and complete the missing parts so that it creates the output above. Note that the columns of numbers do not need to line up perfectly. Run your program in python3 to test your work. HINT: Row i has i columns.
def triangle(): value = 1 row = 1 while row <= __________: column = 1 while column <= __________: if column != __________: print(value, end=" ") else: print(value, end="\n") value = value + 1 column = column + 1 row = row + 1
EXTRA: Try to print out a triangle with 15 rows and get the numbers to line up in neat columns.