15110 SUMMER SESSION TWO - 2014

Lab 3 - Monday, July 7

Deliverables

  1. Paper containing:
    1. trace of doomday algorithm
    2. "legal actions" flow chart and table
  2. doomsday.py
  3. legal.py
  4. triangular.py

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.

CA Demonstration (as necessary)

Activities

  1. 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):

    1. Let y be the last two digits of the year. (How do you compute this using the modulo (%) operator?)
    2. Let a be the integer quotient when you divide y by 12.
    3. Let b be the integer remainder when you divide y by 12.
    4. Take the remainder from the previous step and divide it by 4, keeping just the integer quotient as c.
    5. Add up a, b, and c to form the value d.
    6. Compute e, representing the day of the week for the doomsdays, by adding 2 to d and then dividing by 7, keeping the remainder only. (Note that e should be a value between 0 and 6. Why?)
    7. Return the string shown below given the final value of e:
      ereturn
      0"Sunday"
      1"Monday"
      2"Tuesday"
      3"Wednesday"
      4"Thursday"
      5"Friday"
      6"Saturday"
      otherwise"unknown"

    1. Trace this algorithm on paper for 2014. What day do you get? Check a calendar online to verify that you got the correct answer.

    2. 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?)

  2. 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.

    1. 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.

    2. 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.

    3. 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.

      OutputAgeMale?US Citizen?
      DINK, VOTE & DRAFTED25TrueTrue
      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.

  3. 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.