easy +10 pts

Month End Date

Compute the correct last day of any given month, handling leap years.

Write a function `month_end_date(year: int, month: int) -> str` that takes a year (>= 1) and a month (1–12) and returns a string in the format `YYYY-MM-DD` representing the last day of that month (e.g. "2024-02-29" for February 2024). The function must correctly account for the Gregorian calendar rules, including leap years.

Constraints

- 1 <= year <= 9999 - 1 <= month <= 12 - Your solution must not rely on any external libraries beyond the Python standard library. - Expected time complexity: O(1).

Example

>>> month_end_date(2024, 2)
'2024-02-29'
>>> month_end_date(2023, 2)
'2023-02-28'
>>> month_end_date(2024, 12)
'2024-12-31'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Remember the leap year rule: divisible by 4, except centuries not divisible by 400.
Use a list of days per month for a common year, then adjust February based on the leap year check.
Format the result using an f-string with zero-padded month and day, e.g. f"{year:04d}-{month:02d}-{day:02d}".
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.