[Tutor] Useless (not interesting) little script

Timothy Wilson wilson@visi.com
Sat, 15 Dec 2001 17:25:18 -0600 (CST)


Hi everyone,

Here's something that I whipped up this afternoon. I've always found it
interesting to look for dates that combine to form little arithmetic
expressions. For example, Jan. 2, 2002 is one because 1 x 2 = '02. Likewise,
Feb. 2, 2004 (2 + 2 = '04) and Dec. 2, 2006 (12 / 2 = '06) also work.

Interestingly, I don't think it's necessary to consider leap years in this
script. I can't think of any combination of 2 and 29 that would make a year
this is a leap year. I put it in anyway because the extra check makes no
observable difference in execution speed (for a 1,000 years or so anyway).

Rob, feel free to add this to Useless Python. (I think it qualifies. :-)

Try to guess how many such dates exist in the next millenium and then run
the program. My guess wasn't very close. :-)

-Tim

#!/usr/bin/env python
#
# calmath.py by Tim Wilson <wilson@visi.com>
# This program looks for dates that fit into a simple arithmetic expression.
# The dates are expressed in the typical American format of 
# <Month> <Day>, <Year>
#
# Some matching dates would include:
#     Jan. 2, 2003   (1 + 2 = 3)
#     Oct. 3, 2007   (10 - 3 = 7)
#     Apr. 10, 2040  (4 x 10 = 40)
#     Aug. 2, 2004   (8 / 2 = 4)
#
# The program correctly takes leap years into account, but I don't think
it's
# actually necessary. I can't think of any combination of 2 and 29 that
would
# be a leap year.

def isLeap(year):
    if year % 400 == 0:
        return 1
    elif year % 10 == 0:
        return 0
    elif year % 4 == 0:
        return 1
    else:
        return 0

def calmath(startYear, endYear):
    datesFound = 0
    monthNames = {1:'Jan.', 2:'Feb.', 3:'Mar.', 4:'April', 5:'May',
6:'June',
                 7:'July', 8:'Aug.', 9:'Sept.', 10:'Oct.', 11:'Nov.',
12:'Dec.'}
    daysInMonths = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30,
                    10:31, 11:30, 12:31}
    for year in range(startYear, endYear+1):
        yr = int(str(year)[-2:])
        if isLeap(year): daysInMonths[2] = 29
        else: daysInMonths[2] = 28
        for month in range(1, 13):
            for day in range(1, daysInMonths[month]+1):
                if month+day == yr:
                    datesFound += 1
                    print "%s %s, %s (%s + %s = '%s)" % \
                    (monthNames[month], day, year, month, day,
str(year)[-2:])
                if month-day == yr:
                    datesFound += 1
                    print "%s %s, %s (%s - %s = '%s)" % \
                    (monthNames[month], day, year, month, day,
str(year)[-2:])
                if month*day == yr:
                    datesFound += 1
                    print "%s %s, %s (%s * %s = '%s)" % \
                    (monthNames[month], day, year, month, day,
str(year)[-2:])
                if float(month)/day == yr:
                    datesFound += 1
                    print "%s %s, %s (%s / %s = '%s)" % \
                    (monthNames[month], day, year, month, day,
str(year)[-2:])
    if datesFound == 0:
        print "\nNo dates found."
    elif datesFound == 1:
        print "\nFound one date between Jan. 1, %s and Dec. 31, %s." % \
        (startYear, endYear)
    else:
        print "\n%s dates found between Jan. 1, %s and Dec. 31, %s." % \
        (datesFound, startYear, endYear)

def main():
    startYear = raw_input("Starting year: ")
    endYear = raw_input("Ending year: ")
    if endYear == '': endYear = startYear # Search one year by hitting <CR>
    calmath(int(startYear), int(endYear))
    
if __name__ == '__main__':
    main()

--
Tim Wilson      |   Visit Sibley online:   | Check out:
Henry Sibley HS |  http://www.isd197.org   | http://www.zope.com
W. St. Paul, MN |                          | http://slashdot.org
wilson@visi.com |  <dtml-var pithy_quote>  | http://linux.com