Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here we write every date of the Gregorian calendar as a list or length 3, of the

ID: 3551978 • Letter: H

Question

Here we write every date of the Gregorian calendar as a list or length 3, of the form [y, m, d], where y, m, d are integers (respectively a year, a month, and a day). So, a date list D has three entries, D[0], D[1],D[2] (for the year, month, and day). We will need the function leapyear (or notleap) as an auxiliary function.


Define a function realdate(date) which returns True if the list date is a real date, and returns Falseotherwise. [also use the month-lengths, which depend on leapyears, etc.] Use the fact that y, m, d are integers, m and d cannot be 0, m cannot be > 12;


I'm a beginner so I'm not allowed to use any predefined definitions from the python library. Thanks

Explanation / Answer

def realdate(date):
    #First check if its leap year
    #check if month is feb then days are right or not
    #else check if month is one of 31 days then it is correct or not
    #similarly check for months of 30 days
    #if it does not matches any of the above return false
    if(leapyear(date[0])):
        if(date[1]==2 and date[2]>0 and date[2]<=29):
            return True
        elif(date[1] in(1,3,5,7,8,10,12) and date[2]>0 and date[2]<=31):
            return True
        elif(date[1] in (4,6,9,11) and date[2]>0 and date[2]<=30):
            return True
        else:
            return False
    else:
        if(date[1]==2 and date[2]>0 and date[2]<=28):
            return True
        elif(date[1] in(1,3,5,7,8,10,12) and date[2]>0 and date[2]<=31):
            return True
        elif(date[1] in (4,6,9,11) and date[2]>0 and date[2]<=30):
            return True
        else:
            return False