Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Doomsday calculator - coded by CMHammond
- from random import randint
- # date = [2018, 12, 11]
- DAYS_OF_WEEK = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
- def is_leap_year(year):
- # Divisible by 4/100/400?
- if year % 4 == 0:
- if not year % 100 == 0:
- return True
- elif year % 400 == 0:
- return True
- # If the year is divisble by 4 and 100, but not 400.
- # OR not divisible by 4 at all.
- return False
- def random_date():
- # The Gregorian calendar was introduced in October 1582 by Pope Gregory XIII.
- # Although, it took time for all of Europe to adopt it, with Greece being the last in 1923.
- date = [randint(1583, 2100)]
- date.append(randint(1, 12))
- # ________________________________________________________
- if date[1] in [1, 3, 5, 7, 8, 10, 12]:
- dayLimit = 31
- elif date[1] in [4, 6, 9, 11]:
- dayLimit = 30
- else:
- # Dealing with leap years, if the month is February.
- if is_leap_year(date[0]) == False:
- dayLimit = 28
- else:
- dayLimit = 29
- # ________________________________________________________
- date.append(randint(1, dayLimit))
- # [YYYY, MM, DD]
- return date
- def display_date(date):
- months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
- "November", "December"]
- suffix = "th"
- if date[2] in [1, 21, 31]:
- suffix = "st"
- elif date[2] in [2, 22]:
- suffix = "nd"
- elif date[2] in [3, 23]:
- suffix = "rd"
- month = months[date[1] - 1]
- return "The %d%s of %s %d" % (date[2], suffix, month, date[0])
- def doomsday(date):
- dDays = {"1": 3, "2": 28, "3": 0, "4": 4, "5": 9, "6": 6, "7": 11, "8": 8, "9": 5, "10": 10, "11": 7,
- "12": 12}
- if is_leap_year(date[0]) and (date[1] in ["01", "02", 1, 2]):
- days = (date[2] - (dDays[ str( date[1] ) + 1] )) % 7
- else:
- days = (date[2] - dDays[ str(date[1]) ] ) % 7
- # Returns a 5, 3, 2 or 0. Works out the century code. 21st century counts as 20 or 2/Tuesday.
- fwts = [0, 5, 3, 2, 0, 5, 3]
- century = date[0] / 100
- # This needs to be improved, not sure how. abs() maybe?
- if century >= 20:
- anchor = fwts[3 + ((century - 20) % 4)]
- else:
- anchor = fwts[3 - ((20 - century) % 4)]
- yearDigits = int(str(date[0])[2:])
- # The actual calculation.
- a = yearDigits / 12
- b = yearDigits % 12
- c = b / 4
- result = (days + anchor + a + b + c) % 7
- return [result, days, anchor, a, b, c]
- # Anchor point (Friday, Wednesday, Tuesday, Sunday) or (5, 3, 2, 0) [%4]
- # Last two digits / 12 = A (Integer) and remainder is B
- # B / 4 = C (Integer)
- # (Days + Anchor + A + B + C) % 7 = Your answer
- # Allow for infinitely large years, ie: 2'909'034. You need to check digits [:len(year - 2)] or 2909000 and the last two.
- # User can enter RANDOM or R, instead of DDMMYYYY
- # Menu has in-depth, step-by-step, quick answer and random date
- # Menu will also allow date to have step-by-step/in-depth or quick answer afterwards.
- def usable_input(date):
- # Turns DDMMYYYY into a list of YYYY, MM, DD.
- dateList = [int(date[4:])]
- dateList.append(int(date[2:4]))
- dateList.append(int(date[:2]))
- # [YYYY, MM, DD]
- return dateList
- def date_check(date):
- if len(date) < 5:
- print "ERROR - Date is invalid.\n"
- menu_answer()
- return
- # Remove seperators (if correctly placed)
- if str(date[2]) in [".", "-", "/"] and str(date[5]) in [".", "-", "/"]:
- date = date[:2] + date[3:5] + date[6:]
- # Testing if the date is valid, even if the user puts a "-" at the start.
- try:
- int(date) and int(date[0])
- except ValueError:
- print "ERROR - Date is invalid.\n"
- menu_answer()
- return
- return usable_input(date)
- def programHeader():
- print "_" * 195
- print " :::::::-. ... ... . : .::::::.:::::::-. :::. .-:. ::-. .,-::::: :::. ::: .,-::::: ... ::: ::: :::. :::::::::::: ... :::::::.. "
- print " ;;, `';, .;;;;;;;. .;;;;;;;. ;;,. ;;; ;;;` ` ;;, `';, ;;`;; ';;. ;;;;' ,;;;'````' ;;`;; ;;; ,;;;'````' ;; ;;; ;;; ;;`;;;;;;;;;;''''.;;;;;;;. ;;;;``;;;;"
- print " `[[ [[,[[ \[[,,[[ \[[,[[[[, ,[[[[,'[==/[[[[,`[[ [[ ,[[ '[[, '[[,[[[' [[[ ,[[ '[[, [[[ [[[ [[' [[[ [[[ ,[[ '[[, [[ ,[[ \[[,[[[,/[[[' "
- print " $$, $$$$$, $$$$$$, $$$$$$$$$$$\"$$$ ''' $ $$, $$c$$$cc$$$c c$$\" $$$ c$$$cc$$$c $$' $$$ $$ $$$ $$' c$$$cc$$$c $$ $$$, $$$$$$$$$c "
- print " 888_,o8P'\"888,_ _,88P\"888,_ _,88P888 Y88\" 888o88b dP 888_,o8P' 888 888,,8P\"` `88bo,__,o, 888 888,o88oo,.__`88bo,__,o,88 .d888o88oo,.__888 888, 88, \"888,_ _,88P888b \"88bo,"
- print " MMMMP\"` \"YMMMMMP\" \"YMMMMMP\" MMM M' \"MMM \"YMmMY\" MMMMP\"` YMM \"\"`mM\" \"YUMMMMMP\"YMM \"\"` \"\"\"\"YUMMM \"YUMMMMMP\"\"YmmMMMM\"\"\"\"\"\"YUMMMYMM \"\"` MMM \"YMMMMMP\" MMMM \"W\""
- print "_" * 195
- print
- def displayMenu():
- print
- print "_" * 60
- print "Choices: (A)nswer, (R)andom, (S)tep-by-step, (I)n-depth"
- def menu_answer():
- date = raw_input("Please enter a date (DDMMYYY): ")
- if date.lower() in ["menu", "m"]:
- menu()
- return
- if date.lower() in ["random", "r"]:
- date = random_date()
- print "Random date chosen:", display_date(date), "\n"
- print DAYS_OF_WEEK[doomsday(date)[0]], display_date(date)
- else:
- date = date_check(date)
- if not date == None:
- print display_date(date), "was a", DAYS_OF_WEEK[doomsday(date)[0]].upper()
- def menu():
- programHeader()
- i = None
- while i not in ["quit", "q", "exit", "e"]:
- displayMenu()
- i = raw_input("> ")
- if i.lower() in ["random", "r"]:
- date = random_date()
- print "Random date chosen:", display_date(date)
- raw_input("\nPress ENTER to show answer\n")
- print display_date(date), "was a", DAYS_OF_WEEK[doomsday(date)[0]].upper()
- elif i.lower() in ["answer", "a"]:
- menu_answer()
- elif i not in ["quit", "q", "exit", "e"]:
- print "Please enter a valid option."
- print "Program terminated."
- exit()
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement