Last active
December 19, 2015 03:18
-
-
Save scampbel/5888834 to your computer and use it in GitHub Desktop.
Answers and code trials and errors for the Gentle Introduction to Python 6.189 MIT Open Courseware moderated by the Mechanical MOOC, June, 2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.