diff --git a/LICENSE b/LICENSE index 68325b6..5b2878c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 A. Hinton +Copyright (c) 2018 Aaron H. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/arrays/README.md b/arrays/README.md index d9c400b..ff7b3e0 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -807,7 +807,7 @@ def remove_element(nums: List[int], val: int) -> int: return length - count ``` -## Replace Elements with Greatest Element on Right Side +## Replace Elements with Greatest Element on Right Side 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. diff --git a/math_problems/README.md b/math_problems/README.md index 18c56d2..84d2878 100644 --- a/math_problems/README.md +++ b/math_problems/README.md @@ -2,6 +2,7 @@ * [Count Primes](#count-primes) * [Fizz Buzz](#fizz-buzz) +* [Leap Year](#leap-year) * [Power of Three](#power-of-three) ## Count Primes @@ -65,6 +66,31 @@ def fizz_buzz(iterations: int) -> List[str]: return output ``` +## Leap Year + +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. + +In the Gregorian calendar, three conditions are used to identify leap years: + +The year can be evenly divided by 4, is a leap year, unless: +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. +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 + +Task + +Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False. + +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. + +Input Format + +* the year to test. + +Output Format + +* The function must return a Boolean value (True/False). Output is handled by the provided code stub. + ## Moving Average Create a class `MovingAverage` to calculate the average of a max amount of numbers (n). diff --git a/math_problems/leap_year.py b/math_problems/leap_year.py new file mode 100644 index 0000000..6e2412c --- /dev/null +++ b/math_problems/leap_year.py @@ -0,0 +1,25 @@ + + +def leap_year(year: int) -> bool: + """ + In the Gregorian calendar, three conditions are used to identify leap years: + + The year can be evenly divided by 4, is a leap year, unless: + 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. + 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 + """ + leap = False + + if year % 4 == 0: + if year % 100 == 0 and year % 400 == 0: + leap = True + elif year % 100 == 0: + leap = False + else: + leap = True + else: + leap = False + + return leap diff --git a/math_problems/tests/test_leap_year.py b/math_problems/tests/test_leap_year.py new file mode 100644 index 0000000..6bc02a1 --- /dev/null +++ b/math_problems/tests/test_leap_year.py @@ -0,0 +1,11 @@ +from math_problems.leap_year import leap_year + + +def test_leap_year(): + assert leap_year(2000) + assert leap_year(2400) + assert not leap_year(1800) + assert not leap_year(1900) + assert not leap_year(2100) + assert not leap_year(2200) + assert not leap_year(2300)