public
Last active

Answers and code trials and errors for the Gentle Introduction to Python 6.189 MIT Open Courseware moderated by the Mechanical MOOC, June, 2013

  • Download Gist
ocw_zellers.py
Python
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
# File: ocw_zellers.py
# Section: OCW #Seq17Jun13
# Name: S.Campbell
# Version: Python 3.3.2
##
## exercise OPT.1 from: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw1.pdf
####
##fn=input('Enter your first name: ')
##ln=input('Enter your last name: ')
print ('Enter a date:')
mo =input('Month? ')
day =input('Day? ')
year=input('Year? ')
##mo = str(6)
##day = str(27)
##year = str(2013)
 
A=(int(mo)-3)%12 + 1 # turn march into month 1, jan,feb into 11,12
B=int(day)
year_adjusted=(int(year))
if ((A-1)%12)+1 > 10 : #handle months>12 correctly? (so 18 is same as June?)
year_adjusted = year_adjusted - 1
C=year_adjusted % 100 # i got confused thinking c = century = WRONG!
D=int(year_adjusted / 100)
 
print (mo, day+',', year, ' = A:B:CD are ', str(A),':',str(B),':',str(C),str(D))
 
 
##Zeller’s algorithm is defined as follows:
##Let A, B, C, D denote integer variables that have the following values:
##A = the month of the year, with March having the value 1, April the
##value 2, . . ., December the value 10, and January and February being
##counted as months 11 and 12 of the preceding year (in which
##case,subtract 1 from C)
##B = the day of the month (1, 2, 3, . . . , 30, 31)
##C = the year of the century (e.g. C = 89 for the year 1989)
##D = the century (e.g. D = 19 for the year 1989)
##Note: if the month is January or February, then the preceding year is used for computation. This is because there
##was a period in history when March 1st, not January 1st, was the beginning of the year.
##Let W, X, Y, Z, R also denote integer variables. Compute their valuesin the following order using integer arithmetic:
##W = (13*A - 1) / 5
##X = C / 4
##Y = D / 4
##Z = W + X + Y + B + C - 2*D
##R = the remainder when Z is divided by 7
 
W = int((13*A - 1) / 5)
X = int(C / 4)
Y = int(D / 4)
Z = int(W + X + Y + B + C - 2*D)
R=Z%7
 
print ("WXYZR=",W,X,Y,Z,R)
#days=('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
#print ("that day is a",days[R])
# I think this is cheating to use a tuple because we haven't learned that yet
# so do what I know so far (except it is python 3 :-)
if R == 0 :
dayofweek="Sunday"
elif R == 1:
dayofweek="Monday"
elif R == 2:
dayofweek="Tuesday"
elif R == 3:
dayofweek="Wednesday"
elif R == 4:
dayofweek="Thursday"
elif R == 5:
dayofweek="Friday"
elif R == 6:
dayofweek="Saturday"
else:
dayofweek="I don't know -- Python must be broken because R is " + str(R)
print ("that day is a",dayofweek)

#This is Python 3! -- forgot to add this comment to the code. This is as much a test of Gist/github as Python, I think.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.