Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- # -*- encoding: utf-8 -*-
- #Leap year verification
- # Ulf Benjaminsson, 2017-01-14
- import sys
- #(year, true if leap year)
- def makeTestCase(a, b):
- return (a, b);
- def makeTestCases():
- return [
- makeTestCase(1800, False),
- makeTestCase(1900, False),
- makeTestCase(2100, False),
- makeTestCase(2200, False),
- makeTestCase(2300, False),
- makeTestCase(2500, False),
- makeTestCase(1600, True),
- makeTestCase(2000, True),
- makeTestCase(2400, True),
- ];
- #In the Gregorian calendar three criteria must be taken into account to identify leap years:
- #The year can be evenly divided by 4;
- #If the year can be evenly divided by 100, it is NOT a leap year, unless;
- #The year is also evenly divisible by 400. Then it is a leap year.
- #Constraints: 1900<n<10**5
- def isLeapYear(year):
- if(year < 1900 or year > 100000):
- raise ValueError("Bad argument: year must be 1900 < n < 10^5, was: {!s}".format(year))
- return (year % 400 == 0 or year % 100 != 0) and (year % 4 == 0);
- def main():
- samples = makeTestCases();
- print('Testing for Leap Year ({:d} tests)'.format(len(samples)));
- for (year, correctAnswer) in samples:
- try:
- result = isLeapYear(year);
- except ValueError as error:
- print(error);
- continue;
- passed = result == correctAnswer;
- print('{!s} {} = {:d} {!s}'.format(
- "Passed:" if passed else "\tFailed:", str(year), result, '.')
- );
- if(not passed):
- print('\tShould be: {:d}'.format(correctAnswer)) ;
- return 0;
- if __name__ == '__main__':
- status = main();
- sys.exit(status);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement