Last Day of Any Month

Medium ⏱ 12 min 58% acceptance ★★★★★ 4.7
Write month_end(year, month) returning the last date of that month — leap-February included — using the roll-over trick: day 1 of the next month minus one day. No calendar module.

Examples

Example 1
Input
month_end(2024, 2)
Output
date(2024, 2, 29)
Explanation

2024 is a leap year.

Constraints

  • No calendar.monthrange.
  • December must roll into January of the next year.

Topics

Date & Timecalendar math

Companies

OracleSAPDeloitte

Hints

Hint 1

Next month: (month % 12) + 1, bumping the year when month == 12.

Hint 2

Subtract timedelta(days=1).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.