ulfben icon

Leap year verification

ulfben | PRO | 01/14/17 01:02:57 PM UTC | 0 ⭐ | 984 👁️ | Never ⏰ | []
Python |

1.75 KB

|

None

|

0 👍

/

0 👎

#! 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);

Comments

  • Rteuixl icon
    04/02/26 04:51:53 PM UTC
    text |

    0 B

    |

    0 👍

    /

    0 👎

    Best t33n active cpx link daily updates!
     Copy and paste the link in a new browser tab and then hit enter!
     https://crazyporn.@xxx/members/157354/?asgtbndr=1&play=true&spon=adworld&zone=400
     Remove the @ 
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎