Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Here is the major function you need to write
- #it takes in an integer and returns True or False
- #depending on whether or not it is a valid date
- #You should probably have it call other functions,
- #for example you may want to create some other
- #function that figures out proper number of
- #days in a month
- def isValidDate(date):
- year = getYear(date)
- month = getMonth(date)
- day = getDay(date)
- if(year < 1752):
- print("year1\n")
- return False
- if(year > 2018):
- print(year)
- print("\n")
- print("year\n")
- return False
- if(day > 31):
- print("day\n")
- return False
- if(day < 1):
- print("day\n")
- return False
- if(month < 1):
- print("month\n")
- return False
- if(month > 12):
- print("month\n")
- return False
- if(month == 4 or month == 6 or month == 9):
- if (day > 30):
- return False
- if(month == 2):
- if(isLeap(date)):
- if (day > 29):
- return False
- else:
- if(day > 28):
- return False
- if(year == 2018):
- if(month == 1):
- if(day > 18):
- return False
- if(year == 1752):
- if(month == 9):
- if(day < 14):
- return False
- else:
- return True
- #Given integer date returns
- #the year as an integer
- def getYear(date):
- return date // 10000
- #Given integer date returns
- #the month as an integer
- def getMonth(date):
- return date // 100 % 100
- #Given integer date returns
- #the day as an integer
- def getDay(date):
- return date % 100
- #Here is the leap year
- #function we did in class
- def isLeap(year):
- leap = False
- if((year % 400) == 0):
- leap = True
- elif((year % 100) == 0):
- leap = False
- elif((year % 4) == 0):
- leap = True
- return leap
- def main():
- #Here are some text cases you should add more
- print('For 20180101 isValidDate returns {}'.format(isValidDate(20180101)))
- print("Expected: True\n")
- print('For 20160229 isValidDate returns {}'.format(isValidDate(20160229)))
- print("Expected: True\n")
- print('For 20150229 isValidDate returns {}'.format(isValidDate(20150229)))
- print("Expected: False\n")
- print('For 20160230 isValidDate returns {}'.format(isValidDate(20160230)))
- print("Expected: False\n")
- print('For 10000703 isValidDate returns {}'.format(isValidDate(10000703)))
- print("Expected: False\n")
- print('For 19700131 isValidDate returns {}'.format(isValidDate(19700131)))
- print("Expected: True\n")
- print('For 19700132 isValidDate returns {}'.format(isValidDate(19700132)))
- print("Expected: False\n")
- print('For 19820331 isValidDate returns {}'.format(isValidDate(19820331)))
- print("Expected: True\n")
- print('For 19820332 isValidDate returns {}'.format(isValidDate(19820332)))
- print("Expected: False\n")
- print('For 19820430 isValidDate returns {}'.format(isValidDate(19820430)))
- print("Expected: True\n")
- print('For 19820431 isValidDate returns {}'.format(isValidDate(19820431)))
- print("Expected: False\n")
- print('For 19800229 isValidDate returns {}'.format(isValidDate(19800229)))
- print("Expected: True\n")
- print('For 19800230 isValidDate returns {}'.format(isValidDate(19800230)))
- print("Expected: False\n")
- print('For 19810228 isValidDate returns {}'.format(isValidDate(19810228)))
- print("Expected: True\n")
- print('For 19810229 isValidDate returns {}'.format(isValidDate(19810229)))
- print("Expected: False\n")
- print('For 19000228 isValidDate returns {}'.format(isValidDate(19000228)))
- print("Expected: True\n")
- print('For 19000229 isValidDate returns {}'.format(isValidDate(19000229)))
- print("Expected: False\n")
- print('For 20000228 isValidDate returns {}'.format(isValidDate(20000228)))
- print("Expected: True\n")
- print('For 20000229 isValidDate returns {}'.format(isValidDate(20000229)))
- print("Expected: True\n")
- print('For 20000230 isValidDate returns {}'.format(isValidDate(20000230)))
- print("Expected: False\n")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement