Advertisement
CMHammond

Doomsday Calculator

Dec 10th, 2018 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.70 KB | None | 0 0
  1. # Doomsday calculator - coded by CMHammond
  2.  
  3. from random import randint
  4.  
  5. # date = [2018, 12, 11]
  6. DAYS_OF_WEEK = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  7.  
  8. def is_leap_year(year):
  9.     # Divisible by 4/100/400?
  10.     if year % 4 == 0:
  11.         if not year % 100 == 0:
  12.             return True
  13.         elif year % 400 == 0:
  14.             return True
  15.  
  16.     # If the year is divisble by 4 and 100, but not 400.
  17.     # OR not divisible by 4 at all.
  18.     return False
  19.  
  20.  
  21. def random_date():
  22.     # The Gregorian calendar was introduced in October 1582 by Pope Gregory XIII.
  23.     # Although, it took time for all of Europe to adopt it, with Greece being the last in 1923.
  24.     date = [randint(1583, 2100)]
  25.     date.append(randint(1, 12))
  26.  
  27.     # ________________________________________________________
  28.     if date[1] in [1, 3, 5, 7, 8, 10, 12]:
  29.         dayLimit = 31
  30.  
  31.     elif date[1] in [4, 6, 9, 11]:
  32.         dayLimit = 30
  33.  
  34.     else:
  35.         # Dealing with leap years, if the month is February.
  36.         if is_leap_year(date[0]) == False:
  37.             dayLimit = 28
  38.  
  39.         else:
  40.             dayLimit = 29
  41.     # ________________________________________________________
  42.  
  43.     date.append(randint(1, dayLimit))
  44.  
  45.     # [YYYY, MM, DD]
  46.     return date
  47.  
  48.  
  49. def display_date(date):
  50.     months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
  51.               "November", "December"]
  52.  
  53.     suffix = "th"
  54.  
  55.     if date[2] in [1, 21, 31]:
  56.         suffix = "st"
  57.     elif date[2] in [2, 22]:
  58.         suffix = "nd"
  59.     elif date[2] in [3, 23]:
  60.         suffix = "rd"
  61.  
  62.     month = months[date[1] - 1]
  63.  
  64.     return "The %d%s of %s %d" % (date[2], suffix, month, date[0])
  65.  
  66.  
  67. def doomsday(date):
  68.     dDays = {"1": 3, "2": 28, "3": 0, "4": 4, "5": 9, "6": 6, "7": 11, "8": 8, "9": 5, "10": 10, "11": 7,
  69.              "12": 12}
  70.  
  71.     if is_leap_year(date[0]) and (date[1] in ["01", "02", 1, 2]):
  72.         days = (date[2] - (dDays[ str( date[1] ) + 1] )) % 7
  73.     else:
  74.         days = (date[2] - dDays[ str(date[1]) ] ) % 7
  75.  
  76.         # Returns a 5, 3, 2 or 0. Works out the century code. 21st century counts as 20 or 2/Tuesday.
  77.         fwts = [0, 5, 3, 2, 0, 5, 3]
  78.         century = date[0] / 100
  79.  
  80.         # This needs to be improved, not sure how. abs() maybe?
  81.         if century >= 20:
  82.             anchor = fwts[3 + ((century - 20) % 4)]
  83.         else:
  84.             anchor = fwts[3 - ((20 - century) % 4)]
  85.  
  86.         yearDigits = int(str(date[0])[2:])
  87.  
  88.         # The actual calculation.
  89.         a = yearDigits / 12
  90.         b = yearDigits % 12
  91.         c = b / 4
  92.  
  93.     result = (days + anchor + a + b + c) % 7
  94.  
  95.     return [result, days, anchor, a, b, c]
  96.  
  97.  
  98. # Anchor point (Friday, Wednesday, Tuesday, Sunday) or (5, 3, 2, 0) [%4]
  99. # Last two digits / 12 = A (Integer) and remainder is B
  100. # B / 4 = C (Integer)
  101. # (Days + Anchor + A + B + C) % 7 = Your answer
  102.  
  103. # Allow for infinitely large years, ie: 2'909'034. You need to check digits [:len(year - 2)] or 2909000 and the last two.
  104. # User can enter RANDOM or R, instead of DDMMYYYY
  105. # Menu has in-depth, step-by-step, quick answer and random date
  106. # Menu will also allow date to have step-by-step/in-depth or quick answer afterwards.
  107.  
  108.  
  109. def usable_input(date):
  110.     # Turns DDMMYYYY into a list of YYYY, MM, DD.
  111.  
  112.     dateList = [int(date[4:])]
  113.     dateList.append(int(date[2:4]))
  114.     dateList.append(int(date[:2]))
  115.  
  116.     # [YYYY, MM, DD]
  117.     return dateList
  118.  
  119.  
  120. def date_check(date):
  121.     if len(date) < 5:
  122.         print "ERROR - Date is invalid.\n"
  123.         menu_answer()
  124.         return
  125.  
  126.     # Remove seperators (if correctly placed)
  127.     if str(date[2]) in [".", "-", "/"] and str(date[5]) in [".", "-", "/"]:
  128.         date = date[:2] + date[3:5] + date[6:]
  129.  
  130.     # Testing if the date is valid, even if the user puts a "-" at the start.
  131.     try:
  132.         int(date) and int(date[0])
  133.     except ValueError:
  134.         print "ERROR - Date is invalid.\n"
  135.         menu_answer()
  136.         return
  137.  
  138.     return usable_input(date)
  139.  
  140.  
  141.  
  142. def programHeader():
  143.     print "_" * 195
  144.     print " :::::::-.      ...         ...     .        :   .::::::.:::::::-.    :::.  .-:.     ::-.    .,-:::::   :::.      :::       .,-:::::  ...    ::: :::      :::. ::::::::::::   ...    :::::::..   "
  145.     print "  ;;,   `';, .;;;;;;;.   .;;;;;;;.  ;;,.    ;;; ;;;`    ` ;;,   `';,  ;;`;;  ';;.   ;;;;'  ,;;;'````'   ;;`;;     ;;;     ,;;;'````'  ;;     ;;; ;;;      ;;`;;;;;;;;;;''''.;;;;;;;. ;;;;``;;;;"
  146.     print "  `[[     [[,[[     \[[,,[[     \[[,[[[[, ,[[[[,'[==/[[[[,`[[     [[ ,[[ '[[,  '[[,[[['    [[[         ,[[ '[[,   [[[     [[[        [['     [[[ [[[     ,[[ '[[,   [[    ,[[     \[[,[[[,/[[['  "
  147.     print "   $$,    $$$$$,     $$$$$$,     $$$$$$$$$$$\"$$$  '''    $ $$,    $$c$$$cc$$$c   c$$\"      $$$        c$$$cc$$$c  $$'     $$$        $$      $$$ $$'    c$$$cc$$$c  $$    $$$,     $$$$$$$$$c    "
  148.     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,"
  149.     print "   MMMMP\"`    \"YMMMMMP\"   \"YMMMMMP\" MMM  M'  \"MMM \"YMmMY\"  MMMMP\"`   YMM   \"\"`mM\"            \"YUMMMMMP\"YMM   \"\"` \"\"\"\"YUMMM  \"YUMMMMMP\"\"YmmMMMM\"\"\"\"\"\"YUMMMYMM   \"\"`  MMM     \"YMMMMMP\" MMMM   \"W\""
  150.     print "_" * 195
  151.     print
  152.  
  153.  
  154. def displayMenu():
  155.     print
  156.     print "_" * 60
  157.     print "Choices: (A)nswer, (R)andom, (S)tep-by-step, (I)n-depth"
  158.    
  159.    
  160.    
  161. def menu_answer():
  162.     date = raw_input("Please enter a date (DDMMYYY): ")
  163.  
  164.     if date.lower() in ["menu", "m"]:
  165.         menu()
  166.         return
  167.  
  168.     if date.lower() in ["random", "r"]:
  169.         date = random_date()
  170.         print "Random date chosen:", display_date(date), "\n"
  171.         print DAYS_OF_WEEK[doomsday(date)[0]], display_date(date)
  172.  
  173.     else:
  174.         date = date_check(date)
  175.         if not date == None:
  176.             print display_date(date), "was a", DAYS_OF_WEEK[doomsday(date)[0]].upper()
  177.  
  178. def menu():
  179.  
  180.     programHeader()
  181.     i = None
  182.     while i not in ["quit", "q", "exit", "e"]:
  183.         displayMenu()
  184.         i = raw_input("> ")
  185.  
  186.         if i.lower() in ["random", "r"]:
  187.             date = random_date()
  188.             print "Random date chosen:", display_date(date)
  189.             raw_input("\nPress ENTER to show answer\n")
  190.             print display_date(date), "was a", DAYS_OF_WEEK[doomsday(date)[0]].upper()
  191.  
  192.         elif i.lower() in ["answer", "a"]:
  193.             menu_answer()
  194.  
  195.         elif i not in ["quit", "q", "exit", "e"]:
  196.             print "Please enter a valid option."
  197.  
  198.     print "Program terminated."
  199.     exit()
  200.  
  201.  
  202. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement