Skip to content

Instantly share code, notes, and snippets.

@benjamingeiger
Last active February 20, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamingeiger/3608a9996f433380d421008bce28dee4 to your computer and use it in GitHub Desktop.
Save benjamingeiger/3608a9996f433380d421008bce28dee4 to your computer and use it in GitHub Desktop.
# average birthdates of the presidents
birthdates = [
(1732, 2, 22), # Washington
(1735, 10, 30), # Adams
(1743, 4, 13), # Jefferson
(1751, 3, 16), # Madison
(1758, 4, 28), # Monroe
(1767, 7, 11), # Adams (JQ)
(1767, 3, 15), # Jackson
(1782, 12, 5), # Van Buren
(1773, 2, 9), # Harrison (WH)
(1790, 3, 29), # Tyler
(1795, 11, 2), # Polk
(1784, 11, 24), # Taylor
(1800, 1, 7), # Fillmore
(1804, 11, 23), # Pierce
(1791, 4, 23), # Buchanan
(1809, 2, 12), # Lincoln
(1808, 12, 29), # Johnson (A)
(1822, 4, 27), # Grant
(1822, 10, 4), # Hayes
(1831, 11, 19), # Garfield
(1829, 10, 5), # Arthur
(1837, 3, 18), # Cleveland
(1833, 8, 20), # Harrison
(1843, 1, 29), # McKinley
(1858, 10, 27), # Roosevelt (T)
(1857, 9, 15), # Taft
(1856, 12, 28), # Wilson
(1865, 11, 2), # Harding
(1872, 7, 4), # Coolidge
(1874, 8, 10), # Hoover
(1882, 1, 30), # Roosevelt (FD)
(1884, 4, 8), # Truman
(1890, 10, 14), # Eisenhower
(1917, 5, 29), # Kennedy
(1908, 8, 27), # Johnson (LB)
(1913, 1, 9), # Nixon
(1913, 7, 14), # Ford
(1924, 10, 1), # Carter
(1911, 2, 6), # Reagan
(1924, 6, 12), # Bush (GHW)
(1946, 8, 19), # Clinton
(1946, 7, 6), # Bush (GW)
(1961, 8, 4), # Obama
(1946, 6, 14) # Trump
]
from datetime import date
from math import sin, cos, pi, atan2
sum_x = sum_y = 0
def isleap(year):
"Return True if the given year is a leap year in the Gregorian calendar."
if year % 4 != 0: return False
if year % 100 != 0: return True
if year % 400 != 0: return False
return True
# For each birthdate:
for datetuple in birthdates:
birthdate = date(*datetuple).timetuple()
# Get the day of the year.
birthdate_of_year = birthdate.tm_yday
# Account for leap years.
yeardays = 366.0 if isleap(birthdate.tm_year) else 365.0
# Turn the day of the year into an angle between 0 and 2pi.
angle = (float(birthdate_of_year) / yeardays) * 2 * pi
# Sum up the components separately.
sum_y += sin(angle)
sum_x += cos(angle)
# Average the components and convert back to a number between 1 and 365.
sum_y /= len(birthdates)
sum_x /= len(birthdates)
average_dayofyear = ((atan2(sum_y, sum_x) / (2 * pi)) * 365) + 1
if average_dayofyear < 0:
average_dayofyear += 365
print(average_dayofyear)
# Prints: 345.9946101235643
# "December 12 is the 346th day of the year (347th in leap years) in the Gregorian calendar."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment