Skip to content

Instantly share code, notes, and snippets.

@betaprojects
Last active July 15, 2021 05:02
Show Gist options
  • Save betaprojects/cf27d07f2977ac6e5c73ce7758afde98 to your computer and use it in GitHub Desktop.
Save betaprojects/cf27d07f2977ac6e5c73ce7758afde98 to your computer and use it in GitHub Desktop.
Project Euler & HackerRank problem 19 solution: Counting Sundays solved using Python
import datetime as dt
delta, c, targetDOW, targetDate = dt.timedelta(days=1), 0, 6, 1
yS, mS, dS, yE, mE, dE = 1901,1,1, 2000,12,31
yearDelta = yE-yS # number of years
yS = (yS%400) + 400 # normalized and non-zero
a = dt.datetime(yS, mS, dS)
b = dt.datetime(yS+yearDelta, mE, dE)
while a <= b:
if a.day==targetDate and a.weekday()==targetDOW:
c+= 1
delta = dt.timedelta(days=7)
a+= delta
print (["Mon","Tue","Wed","Thu","Fri","Sat","Sun"][targetDOW],"on day", targetDate, "of the month:", c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment