Last active
June 23, 2020 13:06
-
-
Save Double-A-92/1f44c1e7b63fbf94ac8141b0f7b4f5de to your computer and use it in GitHub Desktop.
Write a program that prints the next 20 leap years.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Input | |
CURRENT_YEAR = 2017 | |
NOF_LEAP_YEARS = 20 | |
# Functions | |
def is_leap_year(year): | |
if year % 4 > 0: | |
return False | |
elif year % 100 > 0: | |
return True | |
elif year % 400 > 0: | |
return False | |
else: | |
return True | |
def print_next_leap_years(current_year, nof_leap_years): | |
nof_leap_years_found = 0 | |
while nof_leap_years_found < nof_leap_years: | |
if is_leap_year(current_year): | |
nof_leap_years_found += 1 | |
print(current_year) | |
current_year += 1 | |
def print_next_leap_years_with_recursion(current_year, nof_leap_years, nof_leap_years_found = 0): | |
if is_leap_year(current_year): | |
nof_leap_years_found += 1 | |
print(current_year) | |
if nof_leap_years_found < nof_leap_years: | |
print_next_leap_years_with_recursion(current_year + 1, nof_leap_years, nof_leap_years_found) | |
# Main | |
print_next_leap_years(CURRENT_YEAR, NOF_LEAP_YEARS) | |
print_next_leap_years_with_recursion(CURRENT_YEAR, NOF_LEAP_YEARS) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool! Try Lua for a challenge... ;)