Editor

What Day of the Week?

#132 · Simulation · Advanced · 120 XP · about 25 min

Write day_of_week(year, month, day) that returns the weekday of a date as a string: 'Monday', 'Tuesday', ..., 'Sunday'. If the date does not exist, such as 30 February, return 'Invalid date'.

Work it out from the calendar rules yourself, without the datetime or calendar modules:

  • The months have 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31 days, but February has 29 in a leap year.
  • A year is a leap year if it divides by 4, except that a year dividing by 100 is a leap year only if it also divides by 400. So 2000 was a leap year and 1900 was not.
  • Use these rules for every year, even long ago. With them, 1 January of year 1 was a Monday, and 20 October 2026 is a Tuesday.

Examples

day_of_week(2026, 10, 20)    # returns 'Tuesday'
day_of_week(2000, 2, 29)     # returns 'Tuesday'
day_of_week(1900, 2, 29)     # returns 'Invalid date'
day_of_week(1, 1, 1)         # returns 'Monday'

Constraints

  • 1 <= year <= 9999
  • month and day are whole numbers but may be out of range, like month 13 or day 0

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.