## Transcribed Text

Homework 1
Instructions
We use a script that extracts your answers by looking for cells in between the cells containing the exercise
statements. So you
MUST add cells in between the exercise statements and add answers within them and
MUST NOT modify the existing cells, particularly not the problem statement
To make markdown, please switch the cell type to markdown from code) - you can hit 'm' when you are in
command mode - and use the markdown language.
Part 1: The Doomsday Algorithm
The Doomsday algorithm, devised by mathematician J. H. Conway, computes the day of the week any given date
fell on. The algorithm is designed to be simple enough to memorize and use for mental calculation.
Example. With the algorithm, we can compute that July 4, 1776 (the day the United States declared
independence from Great Britain) was a Thursday.
The algorithm is based on the fact that for any year, several dates always fall on the same day of the week,
called the doomsday for the year. These dates include 4/4, 6/6, 8/8, 10/10, and 12/12.
Example. The doomsday for 2016 is Monday, so in 2016 the dates above all fell on Mondays. The doomsday for
2017 is Tuesday, so in 2017 the dates above will all fall on Tuesdays.
The doomsday algorithm has three major steps:
1. Compute the anchor day for the target century.
2. Compute the doomsday for the target year based on the anchor day.
3. Determine the day of week for the target date by counting the number of days to the nearest doomsday.
Each step is explained in detail below.
The Anchor Day
The doomsday for the first year in a century is called the anchor dayfor that century. The anchor day is needed
to compute the doomsday for any other year in that century. The anchor day for a century coan be computed
with the formula:
a
= (5(c
mod 1)
2)
mod 7
The result a corresponds to a day of the week, starting with 0 for Sunday and ending with 6 for Saturday.
Note. The modulo operation (x mod y) finds the remainder after dividing x by y For instance, 12 mod 3 since
= 0
the remainder after dividing 12 by 3is 0. Similarly, 11 mod 7, since the remainder after dividing 11 by 7is 1
= 1
Example. Suppose the target year is 1954, so the century is c = 19. Plugging this into the formula gives
a
= (5(19
mod 1)
I 2)
mod 7
= (5(3)
I 2)
mod 7
= 3.
In other words, the anchor day for 1900-1999 is Wednesday, which is also the doomsday for 1900.
Exercise 1.1. Write a function that accepts a year as input and computes the anchor day for that year's century.
The modulo operator and functions in the nath. module may be useful. Document your function with
a
docstring and test your function for a few different years. Do this in a new cell below this one.
The Doomsday
Once the anchor day is known, let ybe the last two digits of the target year. Then the doomsday for the target
year can be computed with the formula:
d
= 1
mod 7
The result d corresponds to a day of the week.
Note. The floor operation x rounds x down to the nearest integer. For instance, 3.1 = 3 and 3.8 = 3.
Example. Again suppose the target year is 1954. Then the anchor day is a = 3, and y = 54, so the formula gives
d
= 51 1
3) mod 7
= = (51 13 13)
mod 7 = 0.
Thus the doomsday for 1954 is Sunday.
Exercise 1.2. Write a function that accepts a year as input and computes the doomsday for that year. Your
function may need to call the function you wrote in exercise 1.1. Make sure to document and test your function.
The Day of Week
The
final step in the Doomsday algorithm is to count the number of days between the target date and a nearby
doomsday, modulo 7. This gives the day of the week.
Every month has at least one doomsday:
(regular years) 1/10, 2/28
(leap years) 1/11, 2/29
3/21, 4/4, 5/9, 6/6, 7/11, 8/8, 9/5, 10/10, 11/7, 12/12
Example. Suppose we want to find the day of the week for 7/21/1954. The doomsday for 1954 is Sunday, and a
nearby doomsday is 7/11. There are 10 days in July between 7/11 and 7/21. Since 10 mod 7, the date 7/21/1954
= 3
falls 3 days after a Sunday, on a Wednesday.
Exercise 1.3. Write a function to determine the day of the week for a given day, month, and year. Be careful of
leap years! Your function should return a string such as "Thursday" rather than a number. As usual, document
and test your code.
Exercise 1.4. How many times did Friday the 13th occur in the years 1900-1999? Does this number seem to be
similar to other centuries?
Exercise 1.5. How many times did Friday the 13th occur between the year 2000 and today?
Part 2: 1978 Birthdays
Exercise 2.1. The file contains the number of births in the United States for each day in 1978.
Inspect the file to determine the format. Note that columns are separated by the tab character, which can be
entered in Python as \t. Write a function that uses iterators and list comprehensions with the string methods
spliti] and P Ü to convert each line of data to the list format
monts, day,
The elements of this list should be integers, not strings. The function
provided below will
help you load the file.
dot zond birtedeysiti-
the contacts of the file into is string.
Argusents:
file pate The pate to the birtndays file.
Returns:
string: The contants of the birtndays file.
es
Exercise 2.2. Which month had the most births in 1978? Which day of the week had the most births? Which day
of the week had the fewest? What conclusions can you draw? You may find the Counto: class in the
co Loctions module useful.
Exercise 2.3. What would be an effective way to present the information in exercise 2.2? You don't need to write
any code for this exercise, just discuss what you would do.

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.