Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import date
- import random
- import time
- try: input_func=raw_input
- except: input_func=input
- #Coded by Ian Mallett
- # http://pastebin.com/eGWj0TNK
- def calculate_is_leapyear(year):
- is_leapyear = False
- if year%4==0:
- is_leapyear = True
- if year%100==0:
- is_leapyear = False
- if year%400==0:
- is_leapyear = True
- return is_leapyear
- def calculate_weekday(year,month,day):
- #Execute algorithm you should use to mentally calculate any day-of-the-week. It is the easiest known.
- # See http://firstsundaydoomsday.blogspot.com/2009/12/quick-start-guide.html
- # In the below, I mod by 7 after every step, but you can do this all at once at the very end if you'd
- # prefer. An easy way to mod by 7 is to subtract multiples of 7. 7, 14, 21, 28, 35, 42, 49, 56,
- # and 63 are the useful multiples here.
- #============================================
- #1. Calculate if is leapyear
- is_leapyear = calculate_is_leapyear(year)
- #============================================
- #2. Add offset due to year
- T = year % 100 #last two digits of year
- if T%2==1: #last two digits odd
- T += 11
- T //= 2 #"T" is even at this point. Divide it by 2.
- if T%2==1: #"T" is odd
- T += 11 #or add 4, since about to mod
- T %= 7
- #============================================
- #3. Add century's first Sunday (CFS)
- # 0 2 4 5
- #--------------------
- # n/a n/a 1500 1600
- # 1700 1800 1900 2000
- # 2100 2200 2300 etc.
- #The delineation between Julian and Gregorian calendars is fuzzy based on location. E.g. in Italy,
- # where the change from Julian to Gregorian was invented/mandated, it was 1582. In
- # England/colonies, it was 1752. Here's the whole gory thing: http://norbyhus.dk/calendar.php
- is_gregorian = year >= 1582 #Italy-centric
- if is_gregorian: #Newer, Gregorian calendar
- if year>=1500 and year<=1599: T+=4
- elif year>=1600 and year<=1699: T+=5
- elif year>=1700 and year<=1799: T+=0
- elif year>=1800 and year<=1899: T+=2
- elif year>=1900 and year<=1999: T+=4
- elif year>=2000 and year<=2099: T+=5
- elif year>=2100 and year<=2199: T+=0
- elif year>=2200 and year<=2299: T+=2
- elif year>=2300 and year<=2399: T+=4
- else: assert False
- else: #Older, Julian calendar
- T += year // 100
- T %= 7
- #Note that you can memorize the values of "T" up to here for years you care about. For example,
- # for 2015, "T" is 1. Then, if you were given a date in 2015, you could just jump to here.
- #============================================
- #4. Add Month's doomsday
- # If January, February, or March, use memorized values
- # Otherwise:
- # If even-numbered month, use month number
- # If odd-numbered month, use the unordered pairs (5,9) and (7,11) (that is, 5->9,9->5 and 7->11,11->7)
- if month == 1: #January
- if is_leapyear: T+=4
- else: T+=3
- elif month == 2: #February
- if is_leapyear: T+=1
- else: T+=0
- elif month == 3: T+= 0 #March
- elif month == 4: T+= 4 #April
- elif month == 5: T+= 9 #May
- elif month == 6: T+= 6 #June
- elif month == 7: T+=11 #July
- elif month == 8: T+= 8 #August
- elif month == 9: T+= 5 #September
- elif month == 10: T+=10 #October
- elif month == 11: T+= 7 #November
- elif month == 12: T+=12 #December
- else: assert False
- T %= 7
- #============================================
- #5. Subtract total from day
- T = day - T
- T %= 7
- #============================================
- #6. Done! Convert T into weekday and output.
- if T<0: T+=7
- # 0 1 2 3 4 5 6
- weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
- #============================================
- #Check answer using Zeller's Congruence (simpler, but harder to do mentally)
- # See https://en.wikipedia.org/wiki/Zeller%27s_congruence
- m = month
- y = year
- if m<3:
- #See http://stackoverflow.com/questions/16376684/is-there-formula-to-calculate-what-day-is-for-anyday-i-got-stuck-by-project-eul
- m += 12
- y -= 1
- K = y % 100
- J = y // 100
- if is_gregorian:
- h = (day + int(13.0*(m+1)/5.0) + K + int(K/4.0) + int(J/4.0) + 5*J) % 7
- else:
- h = (day + int(13.0*(m+1)/5.0) + K + int(K/4.0) + 5 + 6*J) % 7
- d = (h+6) % 7
- assert weekdays[T] == weekdays[d]
- #============================================
- #Return
- return weekdays[T]
- def output_weekday(year,month,day):
- weekday = calculate_weekday(year,month,day)
- print("Weekday for %04d-%02d-%02d: %s"%(year,month,day,weekday))
- def test():
- #Thursday
- output_weekday(1776,7,4) #United States Independence Day
- #Wednesday
- output_weekday(1895,5,1)
- #Saturday
- output_weekday(1962,8,4)
- #Sunday
- output_weekday(1941,12,7) #Pearl Harbor Day
- #Sunday
- output_weekday(1899,12,31)
- #Monday
- output_weekday(1900,1,1) #The next day
- #Saturday
- output_weekday(1066,10,14) #Battle of Hastings (note Julian calendar)
- def quiz():
- while True:
- year = random.randrange(1582,2399+1,1)
- is_leapyear = calculate_is_leapyear(year)
- month = random.randrange(1,12+1,1)
- if month== 1: day=random.randrange(1,31+1,1)
- elif month== 2:
- if is_leapyear: day=random.randrange(1,28+1,1)
- else: day=random.randrange(1,29+1,1)
- elif month== 3: day=random.randrange(1,31+1,1)
- elif month== 4: day=random.randrange(1,30+1,1)
- elif month== 5: day=random.randrange(1,31+1,1)
- elif month== 6: day=random.randrange(1,30+1,1)
- elif month== 7: day=random.randrange(1,31+1,1)
- elif month== 8: day=random.randrange(1,31+1,1)
- elif month== 9: day=random.randrange(1,30+1,1)
- elif month==10: day=random.randrange(1,31+1,1)
- elif month==11: day=random.randrange(1,30+1,1)
- elif month==12: day=random.randrange(1,31+1,1)
- weekday = calculate_weekday(year,month,day)
- current_date = date.today()
- if year==current_date.year and month==current_date.month and day==current_date.day:
- s = "What day of the week is it today, "
- elif year<current_date.year or (year==current_date.year and (month<current_date.month or (month==current_date.month and day<current_date.day))):
- s = "What day of the week was it on "
- else:
- s = "What day of the week will it be on "
- s += "%04d-%02d-%02d? ENTER to get answer." % (year,month,day)
- t0 = time.time()
- input_func(s)
- print("\tIt is/was a %s."%weekday)
- t1 = time.time()
- print("\t%f seconds"%(t1-t0))
- print("")
- quiz()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement