ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# bigheed's solution

## to Leap in the Python Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 400
``````

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of `raise Exception`, you should write:

``````raise Exception("Meaningful message indicating the source of the error")
``````

## Running the tests

To run the tests, run the appropriate command below (why they are different):

• Python 2.7: `py.test leap_test.py`
• Python 3.4+: `pytest leap_test.py`

Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): `python -m pytest leap_test.py`

### Common `pytest` options

• `-v` : enable verbose output
• `-x` : stop running tests on first failure
• `--ff` : run failures from previous test before running other test cases

For other options, see `python -m pytest -h`

## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `\$EXERCISM_WORKSPACE/python/leap` directory.

You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.

For more detailed information about running tests, code style and linting, please see the help page.

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### leap_test.py

``````import unittest

from leap import is_leap_year

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0

class LeapTest(unittest.TestCase):
def test_year_not_divisible_by_4(self):
self.assertIs(is_leap_year(2015), False)

def test_year_divisible_by_4_not_divisible_by_100(self):
self.assertIs(is_leap_year(1996), True)

def test_year_divisible_by_100_not_divisible_by_400(self):
self.assertIs(is_leap_year(2100), False)

def test_year_divisible_by_400(self):
self.assertIs(is_leap_year(2000), True)

if __name__ == '__main__':
unittest.main()``````
``````def is_leap_year(year):
# Check for:
#   if year not divisible by 4, then leap-year = no
#      else if year is divisible by 100 and 400, then leap-year = yes
#      else leap-year = no
if (year%4 == 0) and (year%100 != 0):
return True
elif ((year%4 == 0) and ((year%100 == 0) and (year%400 == 0))):
return True
else:
return False``````

You can combine and greatly simplify your conditions: it's equivalent to year % 4 == 0 and (year % 100 != 0 or year % 400 == 0).

Then, since all you're doing is returning a value that's the same as the result of the condition, you can simply return the result of the condition, and not worry about the if.

Every function, no matter how simple or well-named it may seem to you, should have a documentation string. It doesn't have to be much, just enough that a person doesn't have to read the code itself to determine if and when they should use the function.

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?