Advertisement
Guest User

weeee

a guest
Jan 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1.  
  2. #Here is the major function you need to write
  3. #it takes in an integer and returns True or False
  4. #depending on whether or not it is a valid date
  5. #You should probably have it call other functions,
  6. #for example you may want to create some other
  7. #function that figures out proper number of
  8. #days in a month
  9.  
  10. def isValidDate(date):
  11.         year = getYear(date)
  12.         month = getMonth(date)
  13.         day = getDay(date)
  14.         if(year < 1752):
  15.                 print("year1\n")
  16.                 return False
  17.         if(year > 2018):
  18.                 print(year)
  19.                 print("\n")
  20.                 print("year\n")
  21.                 return False
  22.         if(day > 31):
  23.                 print("day\n")
  24.                 return False
  25.         if(day < 1):
  26.                 print("day\n")
  27.                 return False
  28.         if(month < 1):
  29.                 print("month\n")
  30.                 return False
  31.         if(month > 12):
  32.                 print("month\n")
  33.                 return False
  34.         if(month == 4 or month == 6 or month == 9):
  35.             if (day > 30):
  36.                 return False
  37.         if(month == 2):
  38.             if(isLeap(date)):
  39.                 if (day > 29):
  40.                     return False
  41.            
  42.             else:
  43.                 if(day > 28):
  44.                     return False
  45.         if(year == 2018):
  46.             if(month == 1):
  47.                 if(day > 18):
  48.                     return False
  49.         if(year == 1752):
  50.             if(month == 9):
  51.                 if(day < 14):
  52.                     return False
  53.        
  54.         else:
  55.                 return True
  56.  
  57. #Given integer date returns
  58. #the year as an integer
  59. def getYear(date):
  60.    return date // 10000
  61.  
  62. #Given integer date returns
  63. #the month as an integer
  64. def getMonth(date):
  65.    return date // 100 % 100
  66.  
  67. #Given integer date returns
  68. #the day as an integer
  69. def getDay(date):
  70.     return date % 100
  71.    
  72. #Here is the leap year
  73. #function we did in class
  74.  
  75. def isLeap(year):
  76.     leap = False
  77.     if((year % 400) == 0):
  78.         leap = True
  79.     elif((year % 100) == 0):
  80.         leap = False
  81.     elif((year % 4) == 0):
  82.         leap = True
  83.     return leap
  84.  
  85.  
  86. def main():
  87.  
  88.     #Here are some text cases you should add more
  89.  
  90.     print('For 20180101 isValidDate returns {}'.format(isValidDate(20180101)))
  91.     print("Expected: True\n")
  92.  
  93.     print('For 20160229 isValidDate returns {}'.format(isValidDate(20160229)))
  94.     print("Expected: True\n")
  95.  
  96.     print('For 20150229 isValidDate returns {}'.format(isValidDate(20150229)))
  97.     print("Expected: False\n")
  98.    
  99.     print('For 20160230 isValidDate returns {}'.format(isValidDate(20160230)))
  100.     print("Expected: False\n")
  101.  
  102.     print('For 10000703 isValidDate returns {}'.format(isValidDate(10000703)))
  103.     print("Expected: False\n")
  104.    
  105.     print('For 19700131 isValidDate returns {}'.format(isValidDate(19700131)))
  106.     print("Expected: True\n")
  107.    
  108.     print('For 19700132 isValidDate returns {}'.format(isValidDate(19700132)))
  109.     print("Expected: False\n")
  110.    
  111.     print('For 19820331 isValidDate returns {}'.format(isValidDate(19820331)))
  112.     print("Expected: True\n")
  113.    
  114.     print('For 19820332 isValidDate returns {}'.format(isValidDate(19820332)))
  115.     print("Expected: False\n")
  116.    
  117.     print('For 19820430 isValidDate returns {}'.format(isValidDate(19820430)))
  118.     print("Expected: True\n")
  119.    
  120.     print('For 19820431 isValidDate returns {}'.format(isValidDate(19820431)))
  121.     print("Expected: False\n")
  122.    
  123.     print('For 19800229 isValidDate returns {}'.format(isValidDate(19800229)))
  124.     print("Expected: True\n")
  125.    
  126.     print('For 19800230 isValidDate returns {}'.format(isValidDate(19800230)))
  127.     print("Expected: False\n")
  128.    
  129.     print('For 19810228 isValidDate returns {}'.format(isValidDate(19810228)))
  130.     print("Expected: True\n")
  131.    
  132.     print('For 19810229 isValidDate returns {}'.format(isValidDate(19810229)))
  133.     print("Expected: False\n")
  134.    
  135.     print('For 19000228 isValidDate returns {}'.format(isValidDate(19000228)))
  136.     print("Expected: True\n")
  137.    
  138.     print('For 19000229 isValidDate returns {}'.format(isValidDate(19000229)))
  139.     print("Expected: False\n")
  140.    
  141.     print('For 20000228 isValidDate returns {}'.format(isValidDate(20000228)))
  142.     print("Expected: True\n")
  143.    
  144.     print('For 20000229 isValidDate returns {}'.format(isValidDate(20000229)))
  145.     print("Expected: True\n")
  146.    
  147.     print('For 20000230 isValidDate returns {}'.format(isValidDate(20000230)))
  148.     print("Expected: False\n")
  149.      
  150.    
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement