Short answer: No––that function will not reliably return the right weekday.
Below is a breakdown of why it fails, followed by a clean, correct solution.
Why the current version is buggy
-
Name-shadowing
• The parameter called tuple hides Python’s built-in tuple() constructor.
• It also holds a string, not a tuple, so the name is misleading.
-
Nonsense month “offset”
• In Zeller/Sakamoto–style formulas you add a numeric offset that depends on the month.
• Your code uses ord(tuple[m]) % 8, i.e. the Unicode code-point of the m-th character in the literal string ' (["upseal.block"]) ' modulo 8.
• Those characters have nothing to do with real month offsets, so the answer is essentially random.
-
Month index is off by one
Python’s ord(tuple[m]) takes the m-th character, but months are 1-based in the calling convention you’re using (m=1 → January). That makes the bug even harder to reason about.
-
Style / clarity issues
• The default value for tuple contains spaces and brackets that serve no purpose.
• The function name and parameters don’t follow PEP-8 (day_of_week, year, month, day).
• If you do want to append "day" to your labels, it’s cleaner to store the full words once and avoid string concatenation during every call.
A correct, compact implementation (Sakamoto algorithm)
def day_of_week(year: int, month: int, day: int) -> str:
"""
Return the weekday name ('Sunday' .. 'Saturday') for Gregorian dates
1583-01-01 and later (works for most practical ranges).
"""
# Month offsets (Jan=0 … Dec=11)
t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
# Treat January and February as months 13 and 14 of the previous year
if month < 3:
year -= 1
index = (year +
year // 4 - year // 100 + year // 400 +
t[month - 1] + day) % 7
days = ['Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday']
return days[index]
Example usage
>>> day_of_week(2024, 6, 5)
'Wednesday'
>>> day_of_week(1776, 7, 4)
'Thursday'
If you prefer to stick with the standard library
import datetime
def day_of_week(year, month, day):
return datetime.date(year, month, day).strftime('%A')
The strftime('%A') directive already returns full weekday names.
Tests (quick sanity check)
assert day_of_week(2024, 6, 5) == 'Wednesday' # today (example)
assert day_of_week(2000, 1, 1) == 'Saturday'
assert day_of_week(1999, 12, 31) == 'Friday'
assert day_of_week(1941, 12, 7) == 'Sunday'
assert day_of_week(2020, 2, 29) == 'Saturday' # leap day
Those all pass with the corrected function.
Take-away
• Use well-known month-offset tables instead of arbitrary strings.
• Don’t shadow built-ins like tuple.
• When in doubt, validate with datetime or another trusted source.