How can I optimize this code
def leapYear(year,year2):
print(1)
if (year%4==0 and year%100!=0 or year%400==0) and year2%4==0 and year2%100!=0 or year2%400==0:
return True
return False
def dValidation(date1,date2):
if leapYear(date1[-1],date2[-1]):
if date1[1]==2 or date2[1]==2:
if date1[0]>29 or date2[0]>29:
return False
if date1[0]>31 or date2[0]>31 or date1[1]>12 or date2[1]>12:
return False
else:
if date1[1]==2 or date2[1]==2:
if date1[0]>28 or date2[0]>28:
return False
if date1[0]>31 or date2[0]>31 or date1[1]>12 or date2[1]>12:
return False
return True
date1=list(map(int,input().split("-")))
date2=list(map(int,input().split("-")))
if dValidation(date1,date2):
if date1[-1]>date2[-1]:
print("First")
elif date2[-1]>date1[-1]:
print("Second")
elif date1[1]>date2[1]:
print("First")
elif date1[1]<date2[1]:
print("Second")
elif date1[0]>date2[0]:
print("First")
elif date1[0]<date2[0]:
print("Second")
else:
print("invalid")
else:
print("Invalid")
Input:- 29-3-2018 15-4-2000 ** where 29-3-2018 is first date 15-4-2000 is second date**
output:-First
Input2:-31-6-2018 30-6-2018 output:-First
Note:- print invalid if dates are invalid
29-2-2018, 2-3-2018
output:-Invalid
import datetime
inputDate = input("Enter the first date in format 'dd/mm/yyyy' : ")
day,month,year = inputDate.split('/')
inputDate = input("Enter the second date in format 'dd/mm/yyyy' : ")
day2,month2,year2 = inputDate.split('/')
isValidDate = True
try :
datetime.datetime(int(year),int(month),int(day))
datetime.datetime(int(year2),int(month2),int(day2))
except ValueError :
isValidDate = False
if(isValidDate) :
print ("Input date is valid ..")
else :
print ("Input date is not valid..")
# date in yyyy/mm/dd format
d1 = datetime.datetime(int(year),int(month),int(day))
d2 = datetime.datetime(int(year2),int(month2),int(day2))
# Comparing the dates will return
# either True or False
print("d1 is greater than d2 : ", d1 > d2)
print("d1 is less than d2 : ", d1 < d2)
print("d1 is not equal to d2 : ", d1 != d2)
print("d1 is equal to d2 : ", d1 == d2)
The below code(@Usama K.Alobaidy) will give an answer along with the error because when he was passing dates to d1 and d2 which are Invalid it will through ValueError. To avoid that we can pass the dl and d2 in try block
import datetime
inputDate = input("Enter the first date in format 'dd/mm/yyyy' : ")
day,month,year = inputDate.split('/')
inputDate = input("Enter the second date in format 'dd/mm/yyyy' : ")
day2,month2,year2 = inputDate.split('/')
isValidDate = True
try :
d1=datetime.datetime(int(year),int(month),int(day))
d2=datetime.datetime(int(year2),int(month2),int(day2))
except ValueError :
isValidDate=False
if(isValidDate) :
if d1>d2:
print("First")
else:
print("Second")
else :
print ("Invalid")
Thanks to @Usama K.Alobaidy for introducing DateTime package, I never used this package, But now I know its importance I will start
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.