The area of the circleCS109 Programming ProjectsWarmup: A shopping listFriday the 13th

Friday the 13th

In western countries, many people think that a Friday the 13th (that is, the 13th day of the month) is a very unlucky day.

In this project we will prove that actually every year contains a Friday the 13th.

More precisely, we will show that if you take the twelve days

January 13, February 13, March 13, April 13, May 13, June 13
July 13, August 13, September 13, October 13, November 13, December 13,

then together they cover all the seven days of the week. So not only does every year contain a Friday the 13th, but also a Tuesday the 13th, a Thursday the 13th, and so on.

To get you started, here are two lists that contain the length of the twelve months, once for normal years, once for leap years:

val monthLength = listOf(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
val leapMonthLength = listOf(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

Start by writing a function dayNumbers, which takes the month lengths, and returns a list containing the day number of January 13, February 13, March 13, April 13, …, December 13. (January 1 has day number 0, December 31 has day number 364 or 365.)

The definition should look like this:

fun dayNumbers(monthDays: List<Int>): List<Int> {
  // fill in here
}

And here are the correct results:

>>> dayNumbers(monthLength)
[12, 43, 71, 102, 132, 163, 193, 224, 255, 285, 316, 346]
>>> dayNumbers(leapMonthLength)
[12, 43, 72, 103, 133, 164, 194, 225, 256, 286, 317, 347]

In the next step, we will gather together days that are the same weekday. We know that January 1 to January 7 (that is, day numbers 0 to 6) cover the seven days of the week. We will count how often each of these weekdays occurs in the list of day numbers.

Write a function weekDays that takes a list of day numbers, and returns a list of seven integers, where slot \(i\) indicates how many of the days are the same weekday as day number \(i\). (So all you have to do is to take the day number modulo 7, that is dayNumber % 7 in Kotlin.)

The function definition should look like this:

fun weekDays(days: List<Int>): List<Int> {
  // fill in here
}

Here are some tests:

>>> weekDays(listOf(0, 1, 3, 5))
[1, 1, 0, 1, 0, 1, 0]
>>> weekDays(listOf(0, 1, 3, 5, 8, 14, 21, 24))
[3, 2, 0, 2, 0, 1, 0]
>>> weekDays(listOf(13, 20, 27, 34, 41, 48, 55))
[0, 0, 0, 0, 0, 0, 7]

Finally, run the following code to obtain proof that every year contains a Friday the 13th. What is your output, and why does it prove the claim?

println(weekDays(dayNumbers(monthLength)))
println(weekDays(dayNumbers(leapMonthLength)))
The area of the circleCS109 Programming ProjectsWarmup: A shopping listFriday the 13th