CS 170 Program 3: Zeller's Congruence

Assignment date: 19 September 2018

Due date: 28 September 2018

This program explores the use of functions and if/elif/else statements.

Your program is going to ask the user for a date (in the form of three integers: month, day, year) and first confirm that the given date is valid (e.g., 9/31/1947 is not a valid date because September (9) has only 30 days), then tell the user the day of the week that the date fell on. For example, if the user gives you the date 1/26/1963, your program should first check that the date is valid (it is), then tell the user it was a Saturday.

You will undoubtedly find it useful to use a formula discovered by a British theologian named Zeller. His formula says:

Day = ((26M-2)/10 + D + Y + Y/4 + C/4 + 5C) % 7

where M is the number of the month, D is the day, Y is the last two digits of the year number and C is the century (the first two digits of the year number). Integer division is used. The result will be a value between 0 and 6, where 0 means Sunday, 1 means Monday, . . . 6 means Saturday. Things are made slightly more complicated by the fact that months have to be numbered starting with March as month 1; January and February are treated as months 11 and 12 of the previous year. We therefore need to adjust the month and year like this:

if month < 3:
  year = year - 1
  month = month + 10
else:
  month = month - 2

A detailed example of the formula for the date: January 25, 1956 (i.e., 1/25/1956)

Day = ((26M-2)/10 + D + Y + Y/4 + C/4 +5C) % 7
   = ((26*11-2)/10 + 25 + 55 + 55/4 + 19/4 + 5*19) % 7
   = (284/10 + 25 + 55 + 13 + 4 + 95) % 7
   = 220 % 7
   = 3 (meaning Wednesday)

Your program needs to use functions. Minimally you should have a function for getting a valid month, day and year from the user, a function for calculating the day of the week, and a function for formatting the output to the user. This means your main program might look something like:

def main():
   month, day, year = getValidDate()
   dayNumber = zeller(month, day, year)
   printDate(dayNumber, month, day, year)
Upload your source code (zeller.py) choosing Program 2 as the assignment.