Python 3.8, 77 67 65 62 61 bytes
-2 bytes thanks to @Bubbler
-3 bytes thanks to @xnor
-1 byte thanks to @PoonLevi's mod by float trick
lambda s:13>(m:=s//100%100)>0<s//1e4<30-[s%4<1,m%-1.76][m!=2]
Input: Date as an integer.
Output: True
or False
if the date is valid or invalid respectively.
How:
Overall approach: return 13 > m > 0 < d < max_date_of_month
where d, m
are date and month respectively.
The max date of month m
is calculated as:
- If
m==2
:30-(s%4<1)
evaluates to29
if the year is divisible by 4, and30
otherwise. Since the year is the last 2 digits of the input, input mod 4 is the same as the year mod 4. - If
m!=2
:30-m%-1.76
evaluates to31.xxx
or30.xxx
###Old solution
Old solution
Python 3.8, 86 83 71 bytes
lambda s:13>(m:=s//100%100)>0<s//1e4<29+[s%4>0,([3,2]*7)[m+m//8]][m!=2]
Input: Date as an integer.
Output: True
or False
if the date is valid or invalid respectively.
How: ([3,2]*7)[m+m//8]
first creates a list storing the max_date + 1
for each month (except February) by repeating [3, 2]
a few times. If the month is August or after, the pattern switches, so we add 1 to the index.