Patrice Koehl
Department of Computer Science
Genome Center
Room 4319, Genome Center, GBSF
451 East Health Sciences Drive
University of California
Davis, CA 95616
Phone: (530) 754 5121
koehl@cs.ucdavis.edu




Introduction to Computers: Fall 2013

Lab9: Programming with Python (III)

This is your third programming assignment!

Obviously, you will still program using Python...

Step 1: Getting ready

Please use IDLE as your interface to using Python. Create a file in which you will save your program. To do this using IDLE:

  • Step 1: In the menu File, choose "New Window". IDLE will then create a new window named "Untitled"
  • Step 2: Again in the menu File, choose "Save as..." and give a name to the file: make sure you add the extension .py;
    for example, you can choose the name: lab6.py.
    With the extension .py, IDLE will recognize the file as containing a Python program, and will color it properly.

Now you are ready to write your program!

Step 2: Generate proper header

It is always worth documenting your program as much as you can!
For example, it is important that the file contains the name of the programmer, as well as the date the file was created.

In your file, add the following lines at the top:

#
#   Author:          Your name
#   Student ID:    Your student ID
#   Date:             Today's date
#

where obviously, "Your name", "Your student ID" and "Today's date" refer to you and the date you are working on this program!

Step 3: Programming

You will now solve two small programming exercises; put your solutions to all problems in the file you are working on, one below the other.

Remember to test your program!

Problem 1: Find the day of the week corresponding to a date

Write a Python program that reads from standard input a date (given with Month, Day, and Year) and compute the corresponding day of the week.

Let us suppose that we have read the date as MONTH, DAY, and YEAR. The calculation of the day of the week is based on the Zeller's congruence formula:


h = ( q + (13*(m+1))/5 + K + K/4 + J/4 + 5*J) (modulo 7)
where:
  • J is the number of the century: J = YEAR/ 100
  • K is the year within the century, with a correction: K= YEAR%100 + C, with C=0 if MONTH>2 (i.e. starting from March) and C=-1 if MONTH = 1 or 2 (January or February)
  • m is a modified month index: m = MONTH if MONTH >2, and m = MONTH+12 if MONTH = 1 or 2
  • q is the day of the month: q = DAY
  • h is the day of the week [where 1 is Sunday]. The modulo in the equation means that we take the remainder of the division by 7 (%7 in Python).
Find number of days between two dates

Write a Python program that reads from standard input two dates (both given with Month, Day, and Year) and compute the corresponding number of days that have elapsed between these two dates.

Let us suppose that we have read the first date as MONTH1, DAY1, and YEAR1. We convert this date into a universal date, the Julian Day Number (JDN) using the formula:


JDN1 = DAY1 + (153*m1+2)/5 + 365*y1 + y1/4 - y1/100 + y1/400 - 32045
where:
  • a1 = (14-MONTH1)/12
  • y1 = YEAR1 + 4800 - a1
  • m1 = MONTH1 + 12*a1 - 3

Step 4: File submission

Once you are sure your program is working, submit your file on Smartsite.

Please submit your report as a single document (you can name this document as you want, but do keep the .py extension).

It is very important to turn in your assignment. If you do not turn in, you will not get your credit.
USE SMARTSITE to save your assignment. If you are still not sure how to do it, ask a TA for help.

Grading:


The maximum score you can get is 50. The breakdown is:


Overall presentation Correct heading (with name, student ID and date) 5 points
Exercise 1 Reading the date
Compute the (correct) day of the week
5 points
15 points
Exercise 2 Reading the two dates
Compute number of days elapsed
10 points
15 points

You will get credit for a question only if it is properly executed...

Do not forget to logout from the lab computers!





  Page last modified 17 December 2015 http://www.cs.ucdavis.edu/~koehl/