Created
August 24, 2014 07:27
-
-
Save jsbain/4a3ed356cfb62fe3e9b2 to your computer and use it in GitHub Desktop.
solve-ccc-riddle.py
This file contains hidden or 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
import datetime, logging | |
def elapsed_datetime(start_datetime, end_datetime = datetime.datetime.now()): | |
return end_datetime - start_datetime # returns a datetime.timedelta | |
def find_day(days_on_earth, ref_datetime=datetime.datetime(2014,8,23), ref_is_start=True): | |
'''find day (datetime obj) corresponding to days_on_earth, tuple of days, hours, min, sec. | |
ref_is_start == True means ref date is start, return end date. itherwise, return start date given this end date | |
''' | |
dt=datetime.timedelta(**dict(zip(['days','hours','minutes','seconds'],days_on_earth))) | |
return (ref_datetime+dt) if ref_is_start else (ref_datetime-dt) | |
def find_nextprime_bday(dob,end): | |
assert(end>=dob) | |
from sympy.ntheory import isprime | |
from sympy.ntheory.generate import nextprime | |
# had birthday if current month/day is larger than bday month/ day | |
had_bday_yet=end.strftime('%m%d')>=dob.strftime('%m%d') | |
age = end.year - dob.year - (1 if had_bday_yet else 0) | |
return age if isprime(age) else nextprime(age) | |
def solve_riddle(): | |
#specific day format with mixed 3 and 4 letters, rather than using %a in strftime | |
weekdaynames=['Mon','Tues','Wed','Thurs','Fri','Sat','Sun'] | |
start_datetime= find_day([20000, 0, 3, 55.76],ref_is_start=False ) #dob | |
answer1=start_datetime.strftime('%Y/%m/%d') | |
#next, check day 0, 10000, 20000, 30000, and grab weekday abbrev | |
check_days=[0, 10000, 20000,30000 ] | |
answer2= '/'.join([weekdaynames[find_day([x, 0, 4],start_datetime).weekday()] for x in check_days]) | |
#finally, check next prime bday for doe 30000 | |
answer3= find_nextprime_bday(start_datetime, find_day([30000,0,4],start_datetime)) | |
return (answer1,answer2,answer3) | |
print solve_riddle() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
('1959/11/20', 'Fri/Tue/Sat/Wed', 83)
I got a huge smile on my face when I saw your answer because you got everything correct except that you missed birthday by just one day. I think this was related to your discussion on the forum about zero-based-counting and off-by-one errors. However, the reason that it made me smile was because you guessed Nov. 19th which is my father's birthday instead of Nov. 20th which is my birthday.
When I was to be born, my mother wanted to give me my father's name (Charles) but my father did not like the idea of Charles Jr. or even worse, Charles II. As a compromise, they agreed that if I was born exactly on his birthday then I would be named Charles Jr. However, I was born one day after his birthday (off-by-one) so Charles is my middle name instead of my first name (thus CCC).
Thanks for making me smile on 20,001st day. What a difference a day makes. ;-)