Skip to content

Instantly share code, notes, and snippets.

@igorlg
Created June 27, 2017 23:08
Show Gist options
  • Save igorlg/f9148aeb62130fe00ceeb69169b6bebd to your computer and use it in GitHub Desktop.
Save igorlg/f9148aeb62130fe00ceeb69169b6bebd to your computer and use it in GitHub Desktop.
Show number of each weekday in a year
from datetime import datetime, timedelta
from tabulate import tabulate
from calendar import day_name
import sys
def wd(d):
return list(day_name)[d]
def sorted_week():
return sorted([(j+1)%7 for j in range(7)], key=lambda n: (n+1)%7)
days = { i: 0 for i in range(7) }
try:
year = int(sys.argv[1])
except:
year = datetime.today().year
d = datetime(year, 1, 1)
while d != datetime(year+1, 1, 1):
days[d.weekday()] += 1
d += timedelta(days=1)
print "Year: %s" % year
print tabulate([[wd(i), days[i]] for i in sorted_week()], headers=['Weekday', 'Count'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment