Last active
December 11, 2015 05:49
-
-
Save alanbriolat/4555189 to your computer and use it in GitHub Desktop.
An ugly Python script to work out what proportion of weekday instances are on odd "day of month" numbers for a given year
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
$ python3 odd_days.py 2012 | |
Mon 0.49056603773584906 | |
Tues 0.5192307692307693 | |
Wed 0.4807692307692308 | |
Thurs 0.5192307692307693 | |
Fri 0.5 | |
Sat 0.5 | |
Sun 0.5094339622641509 | |
$ python3 odd_days.py 2013 | |
Mon 0.5 | |
Tues 0.49056603773584906 | |
Wed 0.5192307692307693 | |
Thurs 0.5 | |
Fri 0.5192307692307693 | |
Sat 0.5 | |
Sun 0.5 |
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
#!/usr/bin/env python3 | |
import sys | |
import datetime | |
from collections import defaultdict | |
def generate_year(year): | |
delta = datetime.timedelta(days=1) | |
date = datetime.date(year, 1, 1) | |
while date.year == year: | |
yield date | |
date = date + delta | |
day_numbers = defaultdict(list) | |
for date in generate_year(int(sys.argv[1])): | |
day_numbers[date.isoweekday()].append(int(date.strftime('%m'))) | |
day_odd_prob = {} | |
for day, nums in day_numbers.items(): | |
odd = 0 | |
even = 0 | |
for n in nums: | |
if n % 2 == 0: | |
even += 1 | |
else: | |
odd += 1 | |
day_odd_prob[day] = odd / (odd + even) | |
DAYS = ('Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun') | |
for day in sorted(day_odd_prob.keys()): | |
print('{}\t{}'.format(DAYS[day-1], day_odd_prob[day])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment