def create_from_Y(self, year): month = 7 if is_leap_year(year): day = 2 self.fuzziness = self.fuzziness + timedelta(days=366/2.0) else: day = 2.5 self.fuzziness = self.fuzziness + timedelta(days=365/2.0) self.create_from_YMD(year, month, day)
def create_from_Y(self, year): month = 7 if is_leap_year(year): day = 2 self.fuzziness = self.fuzziness + timedelta(days=366 / 2.0) else: day = 2.5 self.fuzziness = self.fuzziness + timedelta(days=365 / 2.0) self.create_from_YMD(year, month, day)
def solve(self): sum = 0 mod = self.firstMod for year in xrange(self.first, self.end + 1): if is_leap_year(year): self.month[1] = 29 for m in self.month: mod = (mod + m) % 7 if year >= self.start and mod == 0: sum += 1 self.month[1] = 28 if mod % 7 == 0: sum -= 1 return sum
def leap_year(self): return is_leap_year(self.year)
def add_gigasecond(num): gigasecond = 10**9 date_string = str(num) date_split = date_string.split("-") date_int = [int(x) for x in date_split] year = date_int[0] year += (gigasecond // (3600 * 24 * 365) ) #adding the birth year and how many years are in a gigasecond month = date_int[1] day = date_int[2] days_in_month = { 1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 } gigasecond_min_year = gigasecond - ( 31 * (3600 * 24 * 365)) #the remainder of gigasecond minus 31 years days_per_giga = ( gigasecond_min_year // (3600 * 24) ) - 8 #removing the 8 extra days from 8 leap years found in time span d = 1 while d <= days_per_giga: if not is_leap_year( year): #not sure if there's a way to condense this part? if day < days_in_month[month]: day += 1 else: if month == 12: month, day = 1, 1 year += 1 else: month += 1 day = 1 else: days_in_month[2] = 29 if day < days_in_month[month]: day += 1 else: if month == 12: month, day = 1, 1 year += 1 else: month += 1 day = 1 d += 1 return date(year, month, day)
# Feb 1st 1900 was a Thursday assert dow[31 % 7] == 'Thursday' # Jan 1st 1901 was a Tuesday assert dow[365 % 7] == 'Tuesday' result = 0 for year in xrange(1901, 2001): for i, month in enumerate(months): if dow[index % 7] == 'Sunday': print '{} 1st {} was on a Sunday'.format(month, year) result += 1 # Move forward one month # Special case for february if i == months.index('Feb'): index += dpm[i][1] if is_leap_year(year) else dpm[i][0] else: index += dpm[i] print 'Jan 01 2001 was a {}'.format(dow[index % 7]) runtime = time.time() - t0 print 'Result = {}'.format(result) print 'Runtime = {}'.format(runtime)