Skip to content

Instantly share code, notes, and snippets.

@sbancuz
Created September 16, 2022 17:37
Show Gist options
  • Save sbancuz/5feb2b92b4ca1e1a7b7c373ef8975132 to your computer and use it in GitHub Desktop.
Save sbancuz/5feb2b92b4ca1e1a7b7c373ef8975132 to your computer and use it in GitHub Desktop.
Doomsday method practice script
#
# Link to Numberphile video that explains the algorithm to calculate the day of the week in your head
# https://www.youtube.com/watch?v=z2x3SSBVGJU&t=64s
#
# In this script i'm using a different methid called the Zeller’s congruence
# https://en.wikipedia.org/wiki/Zeller%27s_congruence
#
import random
def is_leap_year(year):
if (year % 400 == 0) and (year % 100 == 0):
return True
elif (year % 4 ==0) and (year % 100 != 0):
return True
else:
return False
good_guesses = 0
for i in range(1, 1000):
month = random.randint(1, 12)
year_fst = random.randint(17, 22)
year_scn = random.randint(0, 99)
year = year_fst * 100 + year_scn
if (month in [1, 3, 5, 7, 8, 10, 12]):
day = random.randint(1,31)
elif (month in [4, 6, 9, 11]):
day = random.randint(1,30)
elif (is_leap_year(year)):
day = random.randint(1,29)
else:
day = random.randint(1,28)
days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
month -= 2
if month == -1:
month = 12
elif month == -2:
month = 11
F= day + (((13*month)-1)//5) +year_scn+ (year_scn//4) +(year_fst//4)-2*year_fst
if month == 12:
month = -1
elif month == 11:
month = -2
month += 2
res = int(F) % 7
while True:
guess = input(f"{day}:{month}:{year} is a? ")
if guess.lower() in [day.lower() for day in days]:
if guess.lower() == days[res].lower():
print("OK")
good_guesses += 1
else:
print(f"NAY it was a {days[res]}")
print(f"Success rate of {(good_guesses/i)*100}% (good_guesses: {good_guesses}/ total_guesses: {i})")
break
else:
print(f"Invalid input, the valid inputs are {days}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment