Was too bored in holidays...here's a code which finds the day of the week using the algorithm in this video. Please give a logical date in the correct format cause I didn't account for improper inputs cause I'm too lazy. My code is short and sweet.
day=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"] #some database numbers and strings
mdoom=[3,28,14,4,9,6,11,8,5,10,7,12]
while True:
entry=input('\nEnter a date in DD/MM/YYYY format:') #Input date in a specified format
date,month,year = map(int,entry.split('/')) #Get the date,month and year
centdoom=[3,2,0,5]
a=centdoom[((year//100)%4)-3]
b=(year%100)//12
c=(year%100)%12
d=c//4 #Random calculations for doomsday :)
e=a+b+c+d
if (year%4==0) and ((year%100!=0) or (year%400==0)): #Checks for leap year
mdoom[0],mdoom[1]=4,29
ddoom=e%7 #finds a date in the 1st week which has the same day as the doomsday
r=mdoom[month-1] #Gives the doomsdate in the input month
r=r%7
r=ddoom-r
date=date%7 #finds a date in the 1st week which has the same day as the input date
print('\n',day[(r+date)%7]) #prints the output
k=input('\nTry again?(y/n)')
if k=='y':
continue
else:
break
And any suggestions or improvements are welcome.
-
1\$\begingroup\$ I would slightly improve on leap year (en.wikipedia.org/wiki/Leap_year#Algorithm). \$\endgroup\$dfhwze– dfhwze2019年05月18日 13:07:12 +00:00Commented May 18, 2019 at 13:07
-
\$\begingroup\$ can you please send the edited part of the leap year condition...cause I can't plug in the 2 new conditions according to Wiki in my code.....I'm a complete noob :( ...thanks in advance \$\endgroup\$random_npc– random_npc2019年05月18日 17:48:33 +00:00Commented May 18, 2019 at 17:48
1 Answer 1
Your code is hard to understand, even after watching the video.
- Make some functions.
years_doomsday
would help move some of the hard to understand information to be self-contained. - If you need to floor divide and get the remainder use
divmod
. - You can check the leap year using
calendar.isleap
. - You have a bug, if you ever enter a leap year then the non-leap years will return incorrect values for January and February.
- You should make a function that calls
years_doomsday
, and returns the weekday. - You should make
centdoom
a global constant. - By rotating
centdoom
once you can remove the need for the-3
.
import calendar
WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
MDOOMSDAY = [3, 28, 14, 4, 9, 6, 11, 8, 5, 10, 7, 12]
MDOOMSDAY_LEAP = list(MDOOMSDAY)
MDOOMSDAY_LEAP[:2] = [4, 29]
CDOOMSDAY = [2, 0, 5, 3]
def years_doomsday(year):
a, b = divmod(year, 100)
c, d = divmod(b, 12)
e = d // 4
return (CDOOMSDAY[a % 4] + c + d + e) % 7
def doomsday(year, month, day):
y_doomsday = years_doomsday(year)
mdoomsday = MDOOMSDAY_LEAP if calendar.isleap(year) else MDOOMSDAY
return WEEKDAYS[(day - mdoomsday[month-1] + y_doomsday) % 7]
if __name__ == '__main__':
tests = [
(2305, 7, 13, 'Thursday'),
(1776, 7, 4, 'Thursday'),
(1969, 7, 20, 'Sunday'),
(1984, 1, 6, 'Friday'),
(1902, 10, 19, 'Sunday'),
]
for test in tests:
if doomsday(*test[:3]) != test[-1]:
print('Broken for', test)
If you don't want any fun, you can replace your code with datetime.date.isoweekday
.
import datetime
for test in tests:
if WEEKDAYS[datetime.date(*test[:3]).isoweekday()%7] != test[-1]:
print('Broken for', test)
-
\$\begingroup\$ Nice answer! +1 \$\endgroup\$Justin– Justin2019年06月14日 13:22:36 +00:00Commented Jun 14, 2019 at 13:22
-
\$\begingroup\$ why not do
tests = [(2705, 7, 13), "Thursday",...]
or even making it a dict, and thenfor date, answer in tests:if doormsday(*date) != answer:print(...)
; and I find your way of assemblingMDOOMSDAY_LEAP
rather strange \$\endgroup\$Maarten Fabré– Maarten Fabré2019年06月14日 13:25:56 +00:00Commented Jun 14, 2019 at 13:25 -
1\$\begingroup\$ @MaartenFabré Because I find
((1, 2, 3), 'd')
is more annoying to type then(1, 2, 3, 'd')
. \$\endgroup\$2019年06月14日 13:28:50 +00:00Commented Jun 14, 2019 at 13:28 -
\$\begingroup\$ it is 2 more brackets to type, yes, but a lot more clearer when using the test \$\endgroup\$Maarten Fabré– Maarten Fabré2019年06月14日 13:29:28 +00:00Commented Jun 14, 2019 at 13:29
-
1\$\begingroup\$ @MaartenFabré And I was testing this in IDLE where I kept having to write the same stuff over and over. \$\endgroup\$2019年06月14日 13:29:54 +00:00Commented Jun 14, 2019 at 13:29
Explore related questions
See similar questions with these tags.