Last active
December 20, 2018 17:37
-
-
Save yurivictor/09b40998fa1f41045d29 to your computer and use it in GitHub Desktop.
How many february's are a perfect rectangle
This file contains hidden or 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
from datetime import * | |
from dateutil.rrule import * | |
from dateutil.relativedelta import * | |
PRETTY_YEARS = [] | |
def get_pretty_february(): | |
# look back 100 years | |
YEARS_AGO = 100 | |
TODAY = date.today() | |
YEARSAGO = TODAY + relativedelta( years = -YEARS_AGO ) | |
# check which february 1 started on a sunday | |
feb_one = list( rrule( MONTHLY, bymonth = ( 2 ), bymonthday = ( 1 ), byweekday = ( SU ), dtstart = YEARSAGO, until = TODAY ) ) | |
for x in feb_one: | |
year = int( x.strftime( '%Y' ) ) | |
year_check = is_leap_year( year ) | |
print 'In the past', YEARS_AGO, 'years, there have been', len( PRETTY_YEARS ), 'pretty Februarys' | |
print 'The last pretty February was', PRETTY_YEARS[-2] | |
print PRETTY_YEARS | |
def is_leap_year( year ): | |
"""Given a year as an integer (e.g. 2004) it returns True if the year is a leap year, | |
and False if it isn't.""" | |
if year%4 != 0: | |
PRETTY_YEARS.append( year ) | |
elif year%100 !=0: | |
return True | |
elif year%400 == 0: | |
return True | |
else: | |
PRETTY_YEARS.append( year ) | |
get_pretty_february() | |
# In the past 100 years, there have been 11 pretty Februarys | |
# The last pretty February was 2009 | |
# [1925, 1931, 1942, 1953, 1959, 1970, 1981, 1987, 1998, 2009, 2015] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment