1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#!/usr/bin/env python3
# Challenge 038
#
# TASK #1
# Date Finder
# Create a script to accept a 7 digits number, where the first number can only
# be 1 or 2. The second and third digits can be anything 0-9. The fourth and
# fifth digits corresponds to the month i.e. 01,02,03...,11,12. And the last
# 2 digits respresents the days in the month i.e. 01,02,03...29,30,31. Your
# script should validate if the given number is valid as per the rule and then
# convert into human readable format date.
#
# RULES
# If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to
# make it 4-digits year.
#
# The 4th and 5th digits together should be a valid month.
#
# The 6th and 7th digits together should be a valid day for the above month.
#
# For example, the given number is 2230120, it should print 1923-01-20.
from datetime import date
from calendar import monthrange
import re
import sys
input = sys.argv[1]
if not (m := re.match(r"^([12])(\d\d)([01]\d)([0-3]\d)$", input)):
print("malformed input")
else:
year_msb = int(m.group(1))
year_lsb = int(m.group(2))
month = int(m.group(3))
day = int(m.group(4))
if year_msb == 1:
year = 2000+year_lsb
else:
year = 1900+year_lsb
if not 1 <= month <= 12:
print("malformed month")
else:
if not 1 <= day <= monthrange(year, month)[1]:
print("malformed day")
else:
dt = date(year=year, month=month, day=day)
print(dt.strftime("%Y-%m-%d"))
|