Problem 280313 · medium · Phase 02 Linear Data Structures

Is That a Real Date?

strings · validation · conditionals

Write is_valid_date(s) that returns True if s is a real calendar date written as YYYY-MM-DD:

  • exactly 10 characters: four digits, -, two digits, -, two digits
  • year between 0001 and 9999
  • month between 01 and 12
  • day between 01 and the number of days in that month; February has 29 days in a leap year (divisible by 4, except centuries unless divisible by 400)

Examples

Input:  s = "2024-02-29"
Output: True

Input:  s = "2023-02-29"
Output: False

Input:  s = "1999-12-31"
Output: True

Constraints

  • 0 <= len(s) <= 20, printable ASCII
  • Do not use the datetime module

Goals

  • Check a fixed-width format position by position
  • Convert validated digit groups to numbers
  • Apply the leap-year rule to February
Starting Python…