Skip to main content

Friday 13th

The challenge​

Write a program that determines whether the 13th day of a month falls on a Friday more often than it falls on any other day of the week, given that 1 January 1973 was a Monday.

Aim for the clearest possible program, not the fastest or the cleverest. If your language has a standard date/calendar library, prefer it over hand-rolled date arithmetic — clarity beats reinventing the wheel here.

Remember to follow the red-green-refactor cycle and, if you're using an AI coding assistant, keep it on a tight leash — one test at a time.

Breaking the problem down​

This kata has more moving parts than it first looks like, which makes it a good candidate for building up your solution from several small, independently-tested pieces rather than one big function. Consider splitting it into:

  1. A function that, given a year and month, returns the day of the week the 13th falls on.
  2. A function that tallies how many times the 13th falls on each day of the week across a range of years.
  3. A function that decides whether Friday's tally is strictly higher than every other day's tally.

Suggested test list​

  • Given the anchor date (1 January 1973 is a Monday), the 13th of January 1973 should be a specific, known day of the week — work out what it should be by hand first, so you have a test with a known-correct expected value.
  • The 13th of a month exactly one week after a known 13th should land on the same day of the week.
  • Across a full non-leap year, each month's 13th should be computable without errors, including December (year rollover).
  • Across a range that includes a leap year, February's extra day should shift the day of the week for every 13th from March onwards that year.
  • Tallying across a single, hand-checkable year should produce a tally where all seven counts sum to 12 (one per month).
  • Given a tally where Friday is strictly the highest, the "is Friday most common" check should return true.
  • Given a tally where another day ties with or beats Friday, the check should return false.
  • What should happen if the tally is empty (zero years requested)? Decide deliberately and write a test for it.

Your task​

Implement the solution using strict TDD: write one test from your list, watch it fail, write the minimum code to pass it, refactor, then move to the next test.

💡 Show a reference solution (only after you've completed your own via TDD)

This example uses Python's standard datetime module rather than hand-rolled arithmetic, since a real calendar library is the clearest way to answer this question. date.weekday() returns 0 for Monday through 6 for Sunday.

from datetime import date

WEEKDAY_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"]
FRIDAY = 4

def thirteenth_weekday(year, month):
"""Day of week (0=Monday..6=Sunday) that the 13th falls on."""
return date(year, month, 13).weekday()

def tally_thirteenths(start_year, num_years):
"""Count how many times the 13th falls on each weekday across num_years."""
counts = [0] * 7
for year in range(start_year, start_year + num_years):
for month in range(1, 13):
counts[thirteenth_weekday(year, month)] += 1
return counts

def is_friday_most_common(counts):
highest = max(counts)
return counts[FRIDAY] == highest and counts.count(highest) == 1

if __name__ == "__main__":
# A full Gregorian calendar cycle repeats every 400 years.
counts = tally_thirteenths(1973, 400)
for name, count in zip(WEEKDAY_NAMES, counts):
print(f"{name}: {count}")
print("Friday is the most common weekday for the 13th:",
is_friday_most_common(counts))

Run over a full 400-year Gregorian cycle, you should find Friday comes out very slightly ahead of the other days — a fun, counter-intuitive result of how the Gregorian calendar's leap year rules interact with the 7-day week.