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:
- Input / Output
- Math module
- Conditionals
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.
- Find the last 2 digits of the year. Divide this by four (using integer division)
and sum these two together to get
y
. - Get the corresponding offset (shown below) for the month for
m
. - Take the day to get
d
- Add
y
,m
andd
together to finddow
. dow
mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
---|---|---|---|---|---|---|---|---|---|---|---|
6 | 2 | 2 | 5 | 0 | 3 | 5 | 1 | 4 | 6 | 2 | 4 |
- 17 (last 2 digits of year) + 4 (last two digits, divided by 4) = 21
- 4 (month offset for September)
- 15 (day of month)
- 21 + 4 + 15 = 40
- 40 % 7 = 5 for Friday
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.
- A total solar eclipse occurred over the continental United States on Monday, August 21, 2017.
- The Mesoamerican Long Count calendar used by the Mayans began a new cycle on Friday, December 21, 2012. Some people thought this would be the end of the world.
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.
1700 - 1799 | 1800 - 1899 | 1900 - 1999 | 2000 - 2099 | 2100 - 2199 | 2200 - 2299 |
---|---|---|---|---|---|
5 | 3 | 1 | 0 | -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.
- Find the last 2 digits of the year. Divide this by four (using integer division)
and sum these two together to get
y
. - Get the corresponding offset (shown above) for the month for
m
. - Take the day to get
d
- Add
y
,m
andd
together to finddow
. - Add the year offset (shown below) to
dow
. dow
mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
- George Washington died on Saturday, December 14, 1799.
- Ada Lovelace was born on Sunday, December 10, 1815.
- The Arecibo message was beamed into space on Saturday, 16 November 1974.
- Venus will eclipse Jupiter on Tuesday, September 14, 2123.
- The Enterprise begins its five-year mission on Monday, January 2, 2265.
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.- Find the last 2 digits of the year. Divide this by four (using integer division)
and sum these two together to get
y
. - Get the corresponding offset (shown above) for the month for
m
. - Take the day to get
d
- Add
y
,m
andd
together to finddow
. - Add the year offset (also shown above) to
dow
. - If the year is a leap year and the month is January or February, subtract 1 from
dow
. dow
mod 7 corresponds to the day of the week. (0 = Sunday, 1 = Monday, 2 = Tuesday, etc.)
- Napoleon Bonaparte was elected First Consul of France on Friday, February 7, 1800.
- Vienna debut of Beethoven's Piano Concerto No. 5 was performed by his student Carl Czerny on Wednesday, Feburary 12, 1812.
- Puccini's opera Tosca premiered in Rome Italy on Sunday, January 14, 1900.
- Groundhog Day, Sunday, February 2, 1992.
- The Tagish Lake Meteorite lands on Tuesday, January 18, 2000.
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 submission form. Make sure you have followed the Python Style Guide, and have run your code through the Automated Style Checker.You must hand in:
- dow.py
© Mark Goadrich, Hendrix College