Skip to content

Instantly share code, notes, and snippets.

@TosinAF
Created December 2, 2013 08:07
Show Gist options
  • Save TosinAF/7746478 to your computer and use it in GitHub Desktop.
Save TosinAF/7746478 to your computer and use it in GitHub Desktop.
#python3
import itertools, datetime
def readFile(path):
""" Requires only one arguement, the path of the file to be read """
with open(path) as file: #auto closes file after
content = file.readline()
return content
def set_up(date):
""" Format Date into a lsit of three integers """
date = str(date)
date = date.split('/')
values = []
for i in date:
values.append(int(i))
return values
def solution(date):
""" Main Logic to solve problem """
values = set_up(date)
#Fix the case where one of the integers is 0 (2000)
for i in values:
if i == 0:
values.remove(i)
values.append(2000)
#Numbers that can never be used for anything but the year
if i > 31 and i < 100:
values.remove(i)
values.append(2000 + i)
#Generate all possible dates
perms = list(itertools.permutations(values))
dates = []
#Converting combinations of dates into datetime objects enables
#easy testing for invalid dates, which are ignored (by handling the exception created)
#allows sorting to find the earliest date
for i in perms:
try:
if i[0] < 31 : dates.append(datetime.date(2000 + i[0],i[1],i[2]))
else: dates.append(datetime.date(i[0],i[1],i[2]))
except:
continue
dates.sort()
if dates == []:
return (str(date) + ' is illegal')
else:
return dates[0]
#Run Program
#If you have a file location, Use the commented path below
#date = readFile(path)
#print(solution(date))
#If you have the exact date, Use the commented path below
#print(solution('02/4/67'))
#print(solution('31/9/73'))
date = input('Please input the date:')
print(solution(date))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment