CSCI 150 - Lab 4 - This Day in History

CSCI 150 - Lab 4
This Day in History


Overview

Many systems have been devised over the years for keeping time. In this lab, you will explore some of the oddities present in our current calendar system by using a formula to calculate the day of the week for any given day between 1700 and 2299.

In particular, this lab will review the following concepts we have discussed in class:

Description

Over the centuries, our calendar has been realigned many times to coincide with growth in our astronomical knowledge. Incorporating elements of both the solar and lunar cycle, the current international standard calendar is the Gregorian Calendar, first adopted in 1582. There are seven days a week due to religious influence, 12 months to a year in honor of the lunar cycle, and 365 days a year in relation to the earth's orbital period around the sun, with enough leap days included to properly align the system with the true orbital period of approximately 365.242190419 days. While better than the Egyptian and Roman calendars in use at the time of adoption, it is still an approximation with room for improvement, from the more-precise Iranian Calendar to a standardized and perpetual World Calendar.

One of the more interesting quirks of the Gregorian Calendar is that each year can start on a different day of the week, since 365 is not a multiple of 7. But there is a simple algorithm (based on the Doomsday Rule) that you can memorize to find the day of the week for any date in this century.

  1. Find the last 2 digits of the year. Divide this by four (using integer division) and sum these two together to get y.
  2. Get the corresponding offset (shown below) for the month for m.
  3. Take the day to get d
  4. Add y, m and d together to find dow.
  5. dow mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
Table for Month Offset

JanFebMarAprMayJun JulAugSepOctNovDec
622503 514624

For example, if you wish to know the day of the week for September 15th, 2017, these are the steps you would take:
  1. 17 (last 2 digits of year) + 4 (last two digits, divided by 4) = 21
  2. 4 (month offset for September)
  3. 15 (day of month)
  4. 21 + 4 + 15 = 40
  5. 40 % 7 = 5 for Friday
For this lab, you will write a Python program called dow.py. We will incrementally improve this program, both in accuracy and in style.

Step 1 - Initial Program

Our first version of this program will first tell the user about the program, and then take as input from the user the date they wish to use for finding the day of the week. This input must be in the form of three questions to the user, first the year, then the month (as a number), then the day. For example, you would type in 2017, then 9, then 15 for September 15th, 2017.

With correct input, your program will then parse this input into the year, month and day, follow the above algorithm to compute the day of the week, and output this to the user as a number. So for the above entry, your program will output 5. Test your code on the following dates to ensure you are getting the right answers.

Step 2 - Other Centuries

With a small change, our formula can be extended to work between Jan 1st, 1700 and Dec 31st, 2299. To do this, we need an additional offset table for the year.

Table for Year Offset

1700 - 17991800 - 18991900 - 19992000 - 20992100 - 21992200 - 2299
5310-2-4

Our algorithm is now augmented with an additional step, highlighted below. Change your code to work for any date in the above range.

  1. Find the last 2 digits of the year. Divide this by four (using integer division) and sum these two together to get y.
  2. Get the corresponding offset (shown above) for the month for m.
  3. Take the day to get d
  4. Add y, m and d together to find dow.
  5. Add the year offset (shown below) to dow.
  6. dow mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
Test your code on the following dates to ensure you are getting the right answers.

Step 3 - Leap Years

Our formula has a subtle problem. If the year in question is a leap year, there is an extra day on February 29th! This can be accommodated by adding one more step to our algorithm, again highlighted below.
  1. Find the last 2 digits of the year. Divide this by four (using integer division) and sum these two together to get y.
  2. Get the corresponding offset (shown above) for the month for m.
  3. Take the day to get d
  4. Add y, m and d together to find dow.
  5. Add the year offset (also shown above) to dow.
  6. If the year is a leap year and the month is January or February, subtract 1 from dow.
  7. dow mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
Leap year calculations usually are necessary for every fourth year, but not always. Under the Gregorian Calendar, a year is a leap year (such that February has 29 days) if it is a multiple of four, except when it is also a multiple of 100 but not 400. So, 1900 and 2100 are not leap years, but 2000 is a leap year. Test your code on the following dates to ensure you are getting the right answers.

Step 4 - Formatting

Our code is calculating the correct day of the week, but is not very useful to people. Change your code to produce output in the following format exactly (using September 15th, 2017 as an example):

In the Gregorian Calendar, September 15th, 2017 falls on a Friday.

For dates, use st, nd, rd and th suffixes appropriately. (If English is not your first language, feel free to ask your instructor for help understanding the rules for these suffixes. English is very strange!).

Be sure to use exactly the right input and output formats as described above! You are free to be creative in the text you display when prompting the user, and in any other text your program displays before the final day of the week output, but part of your grade will be based on your adherence to the input and output format requirements. For example, a program receiving full credit might have output like the following:

Welcome to the Day of the Week Finder!
Please enter the year: 2017
Please enter the month, as a number between 1-12: 9
Please enter the day of the month: 15
In the Gregorian Calendar, September 15th, 2017 falls on a Friday.

Step 5 - Testing

Test your code on all the above dates to ensure you are getting the right answers. Also test your birthday, and find three other dates than these for your own testing, one of which is in January or February of a leap year.

What to Hand In

Upload your code using the usual turn-in form. Make sure you have followed the Python Style Guide, and have run your code through the Automated Style Checker.

You must hand in:


© Mark Goadrich, Hendrix College