medium +20 pts

Zeller Congruence Weekday

Compute the day of the week for any Gregorian date using Zeller's congruence.

Write a function `weekday(year: int, month: int, day: int) -> str` that returns the day of the week as one of the strings `"Saturday"`, `"Sunday"`, `"Monday"`, `"Tuesday"`, `"Wednesday"`, `"Thursday"`, or `"Friday"` for the given Gregorian calendar date. Use Zeller's congruence algorithm (the standard formula for the Gregorian calendar). The algorithm works as follows: 1. If the month is January or February, treat it as month 13 or 14 of the previous year. 2. Compute: `h = (q + 13*(m+1)//5 + K + K//4 + J//4 + 5*J) % 7` where: - `q` is the day of the month - `m` is the adjusted month (March=3, ..., December=12; January=13, February=14 of the previous year) - `K` is the year of the century (year % 100) - `J` is the zero-based century (year // 100) 3. The value `h` maps to the weekday as follows: `0` → Saturday, `1` → Sunday, `2` → Monday, `3` → Tuesday, `4` → Wednesday, `5` → Thursday, `6` → Friday. Implement this conversion in the function. You may assume all inputs are valid Gregorian dates (year >= 1583, month 1-12, day valid for the month).

Constraints

Input: 1583 <= year <= 9999, 1 <= month <= 12, 1 <= day <= 31 (valid for the month). The function must handle leap years correctly (e.g., February 29). Time complexity O(1).

Example

[">>> weekday(2023, 10, 9)\n'Monday'", ">>> weekday(2000, 2, 29)\n'Tuesday'", ">>> weekday(2023, 1, 1)\n'Sunday'", ">>> weekday(2023, 12, 31)\n'Sunday'"]
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Remember to adjust January and February as months 13 and 14 of the previous year.
Compute K = year % 100 and J = year // 100 after the month adjustment.
The final formula yields h from 0 to 6; map the result to the weekday names in the order specified.
Test with a known date like 2000-01-01 (Saturday) to verify your algorithm.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.