Skip to content

Commit 54574c0

Browse files
committed
Leap year (#181)
* Leap year * Fix linter error
1 parent 2416e16 commit 54574c0

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎arrays/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ def remove_element(nums: List[int], val: int) -> int:
844844
return length - count
845845
```
846846

847-
## Replace Elements with Greatest Element on Right Side
847+
## Replace Elements with Greatest Element on Right Side
848848

849849
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
850850

‎math_problems/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [Count Primes](#count-primes)
44
* [Fizz Buzz](#fizz-buzz)
5+
* [Leap Year](#leap-year)
56
* [Power of Three](#power-of-three)
67

78
## Count Primes
@@ -65,6 +66,31 @@ def fizz_buzz(iterations: int) -> List[str]:
6566
return output
6667
```
6768

69+
## Leap Year
70+
71+
An extra day is added to the calendar almost every four years as February 29, and the day is called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25 days to orbit the sun. A leap year contains a leap day.
72+
73+
In the Gregorian calendar, three conditions are used to identify leap years:
74+
75+
The year can be evenly divided by 4, is a leap year, unless:
76+
The year can be evenly divided by 100, it is NOT a leap year, unless:
77+
The year is also evenly divisible by 400. Then it is a leap year.
78+
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
79+
80+
Task
81+
82+
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
83+
84+
Note that the code stub provided reads from STDIN and passes arguments to the is_leap function. It is only necessary to complete the is_leap function.
85+
86+
Input Format
87+
88+
* the year to test.
89+
90+
Output Format
91+
92+
* The function must return a Boolean value (True/False). Output is handled by the provided code stub.
93+
6894
## Moving Average
6995

7096
Create a class `MovingAverage` to calculate the average of a max amount of numbers (n).

‎math_problems/leap_year.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
def leap_year(year: int) -> bool:
4+
"""
5+
In the Gregorian calendar, three conditions are used to identify leap years:
6+
7+
The year can be evenly divided by 4, is a leap year, unless:
8+
The year can be evenly divided by 100, it is NOT a leap year, unless:
9+
The year is also evenly divisible by 400. Then it is a leap year.
10+
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years,
11+
while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years. Source
12+
"""
13+
leap = False
14+
15+
if year % 4 == 0:
16+
if year % 100 == 0 and year % 400 == 0:
17+
leap = True
18+
elif year % 100 == 0:
19+
leap = False
20+
else:
21+
leap = True
22+
else:
23+
leap = False
24+
25+
return leap
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from math_problems.leap_year import leap_year
2+
3+
4+
def test_leap_year():
5+
assert leap_year(2000)
6+
assert leap_year(2400)
7+
assert not leap_year(1800)
8+
assert not leap_year(1900)
9+
assert not leap_year(2100)
10+
assert not leap_year(2200)
11+
assert not leap_year(2300)

0 commit comments

Comments
 (0)