Python: leap
This commit is contained in:
parent
2ef852f257
commit
77b3a6a689
4 changed files with 112 additions and 0 deletions
1
python/leap/.exercism/metadata.json
Normal file
1
python/leap/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"track":"python","exercise":"leap","id":"3941a91613fc47449adec3c2db33f5fa","url":"https://exercism.io/my/solutions/3941a91613fc47449adec3c2db33f5fa","handle":"DmitryKokorin","is_requester":true,"auto_approve":false}
|
||||
71
python/leap/README.md
Normal file
71
python/leap/README.md
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# Leap
|
||||
|
||||
Given a year, report if it is a leap year.
|
||||
|
||||
The tricky thing here is that a leap year in the Gregorian calendar occurs:
|
||||
|
||||
```text
|
||||
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.
|
||||
|
||||
## 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][video].
|
||||
|
||||
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
|
||||
|
||||
|
||||
## 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:
|
||||
|
||||
```python
|
||||
raise Exception("Meaningful message indicating the source of the error")
|
||||
```
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests, run `pytest leap_test.py`
|
||||
|
||||
Alternatively, you can tell Python to run the pytest module:
|
||||
`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 [Running the Tests](http://exercism.io/tracks/python/tests).
|
||||
|
||||
## Source
|
||||
|
||||
JavaRanch Cattle Drive, exercise 3 [http://www.javaranch.com/leap.jsp](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.
|
||||
2
python/leap/leap.py
Normal file
2
python/leap/leap.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def leap_year(year):
|
||||
return (0 == year % 4 and 0 != year % 100 ) or (0 == year % 400)
|
||||
38
python/leap/leap_test.py
Normal file
38
python/leap/leap_test.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import unittest
|
||||
|
||||
from leap import leap_year
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json`
|
||||
|
||||
|
||||
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_in_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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue