This is not for a course credit. It’s just online courseware. Although I’d prefer that you people not provide a full solution. Instead just provide guidance, hints, and support in a general direction. Thanks.
Here are the Instructions for this task/challenge course module:
Given a year, report if it is a leap year.
The tricky thing here is that a leap year in the Gregorian calendar occurs:on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.
For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.
There is only one variable - - a given year - - but so many different possible cases and conditions that the unit test tries to verify.
The problem involves basic predicate logic and bivalence. I’ve tried many different combinations of operators, like swapping the three instances of disjunctions and conjunctions, back and forth, one for the other. Every time I change one of the terms, I run the test script, and as a result, a different set of Assertions pass while others fail. I’ve also tried different combinations of equality and inequality operators which will also trigger different Assertions to pass and fail. After each change, I am no closer to achieving the end goal which is to have all of them pass.
For the return
line, if I change True
for False
, the Assertions that previously failed, are now passing, which kind of makes sense.
I’ve done my very best to use the exercise’s description as pseudo code and guide:
on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
But I am missing something and not sure what.
Here is my script:
def leap_year(year):
if (year % 4 == 0 and year % 2 == 0) and (year % 100 == 0) or (year % 400 != 0): # or (year % 100 != 3):
return True
Using that script, here is my unit test trace back passing 4 tests and failing 5 tests:
============ short test summary info ============
FAILED python/leap/leap_test.py::LeapTest::test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year - AssertionError: True is not False
FAILED python/leap/leap_test.py::LeapTest::test_year_divisible_by_100_not_divisible_by_400_in_common_year - AssertionError: True is not False
FAILED python/leap/leap_test.py::LeapTest::test_year_divisible_by_200_not_divisible_by_400_in_common_year - AssertionError: True is not False
FAILED python/leap/leap_test.py::LeapTest::test_year_divisible_by_2_not_divisible_by_4_in_common_year - AssertionError: True is not False
FAILED python/leap/leap_test.py::LeapTest::test_year_not_divisible_by_4_in_common_year - AssertionError: True is not False
========== 5 failed, 4 passed in 0.07s ==========
Here is the unit test class revealing the test conditions:
class LeapTest(unittest.TestCase):
def test_year_not_divisible_by_4_in_common_year(self):
self.assertIs(leap_year(2015), False)
def test_year_divisible_by_2_not_divisible_by_4_in_common_year(self):
self.assertIs(leap_year(1970), False)
def test_year_divisible_by_4_not_divisible_by_100_in_leap_year(self):
self.assertIs(leap_year(1996), True)
def test_year_divisible_by_4_and_5_is_still_a_leap_year(self):
self.assertIs(leap_year(1960), True)
def test_year_divisible_by_100_not_divisible_by_400_in_common_year(self):
self.assertIs(leap_year(2100), False)
def test_year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year(self):
self.assertIs(leap_year(1900), False)
def test_year_divisible_by_400_is_leap_year(self):
self.assertIs(leap_year(2000), True)
def test_year_divisible_by_400_but_not_by_125_is_still_a_leap_year(self):
self.assertIs(leap_year(2400), True)
def test_year_divisible_by_200_not_divisible_by_400_in_common_year(self):
self.assertIs(leap_year(1800), False)