Welcome, guest | Sign In | My Account | Store | Cart

This is a Python progam that lets you practice determining the day of week for any date. This program assumes that you know how to use the Doomsday Algorithm.

Python, 80 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#This program provides practice for the Doomsday Algorithm
#You should know how to use the alg before practicing

from random import *
import time

January = [1,31,31,3,4]
February = [2,28,29,28,29]
March = [3,31,31,7,7]
April = [4,30,30,4,4]
May = [5,31,31,9,9]
June = [6,30,30,6,6]
July = [7,31,31,11,11]
August = [8,31,31,8,8]
September = [9,30,30,5,5]
October = [10,31,31,10,10]
November = [11,30,30,7,7]
December = [12,31,31,12,12]
Months = [January,February,March,April,May,June,July,August,September,October,November,December]
WeekText = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
Years = [1800,2199]

for _ in xrange(5):
    month = Months[randint(0,11)][0]
    year = randint(Years[0],Years[1])

    if (year % 400) != 0 and (year % 4) != 0:
        day = randint(1,Months[month-1][1])
    else:
        day = randint(1,Months[month-1][2])

    print month, "/", day, "/", year

    if year < 1900:
        part1 = 6
        part2 = year - 1800
    elif year < 2000:
        part1 = 4
        part2 = year - 1900
    elif year < 2100:
        part1 = 3
        part2 = year - 2000
    else:
        part1 = 1
        part2 = year - 2100

    part3 = int(part2 / 12)
    part4 = part2 % 12
    part5 = int(part4 / 4)
    doomsDay = part3 + part4 + part5 + part1

    while doomsDay > 7:
        doomsDay = doomsDay - 7

    if month > 2:
        part6 = (day+14) - Months[month-1][3]
        myDay = doomsDay + part6
        while myDay > 7:
            myDay = myDay - 7
    else:
        if (year % 400) != 0 and (year % 4) != 0:
            part6 = (day+35) - Months[month-1][3]
            myDay = doomsDay + part6
            while myDay > 7:
                myDay = myDay - 7
        else:
            part6 = (day+35) - Months[month-1][4]
            myDay = doomsDay + part6
            while myDay > 7:
                myDay = myDay - 7

    start = time.clock()
    guess = raw_input("Enter your guess for the day of the week: ")

    print "The answer is: ", WeekText[int(myDay)-1]
    end = time.clock()

    totalTime = end - start

    print "It took you: ", round(totalTime,2), "seconds."

This is a good tool for adding to your use of worthless information. No one seems to care that within 10 seconds I know what day of the week they were born, but I am satisfied that I know. You could customize the date range, but that creates the need for added coding, and this range is sufficient once you figure out that 2400 has the same doomsday as 2000, and 2500 the same as 2100, etc.

2 comments

Joel Rosdahl 16 years, 3 months ago  # | flag

clock versus time. time.clock() should be time.time() -- otherwise you are measuring CPU time instead of wall clock time, at least on Unix platforms.

Joel Rosdahl 16 years, 3 months ago  # | flag

Alternative. Thanks for mentioning the doomsday algorithm! I hadn't seen it before and it was fun to learn.

A note about the implementation: It's not necessary to implement the algorithm explicitly in a practising program since Python's standard library already knows how to compute the weekday given a date.

Here is a roughly equivalent program that uses batteries already included in Python:

from datetime import date
from random import random
from time import time

start_ord = date(1800, 1, 1).toordinal()
end_ord = date(2199, 12, 31).toordinal()
weekdays = [
    x + "day"
    for x in ["Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "Sun"]]

for _ in xrange(5):
    d = date.fromordinal(start_ord + int(random() * (end_ord - start_ord + 1)))
    print "%04d-%02d-%02d" % (d.year, d.month, d.day) # ISO 8601
    t0 = time()
    raw_input("Enter your guess for the day of the week: ")
    print "The answer is:", weekdays[d.weekday()]
    print "It took you:", round(time() - t0, 2), "seconds.\n"

(I chose to print ISO 8601-style dates -- american-style dates are confusing to me.)