-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlec12.4.py
192 lines (155 loc) · 4.93 KB
/
lec12.4.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# lec12.4-gradebook.py
#
# Lecture 12 - Object Oriented Programming
# Video 4 - Example: A Gradebook
#
# edX MITx 6.00.1x
# Introduction to Computer Science and Programming Using Python
import datetime
class Person(object):
def __init__(self, name):
"""create a person called name"""
self.name = name
self.birthday = None
self.lastName = name.split(' ')[-1]
def getLastName(self):
"""return self's last name"""
return self.lastName
def setBirthday(self,month,day,year):
"""sets self's birthday to birthDate"""
self.birthday = datetime.date(year,month,day)
def getAge(self):
"""returns self's current age in days"""
if self.birthday == None:
raise ValueError
return (datetime.date.today() - self.birthday).days
def __lt__(self, other):
"""return True if self's ame is lexicographically
less than other's name, and False otherwise"""
if self.lastName == other.lastName:
return self.name < other.name
return self.lastName < other.lastName
def __str__(self):
"""return self's name"""
return self.name
# me = Person("William Eric Grimson")
# print me
# me.getLastName()
# me.setBirthday(1,2,1927)
# me.getAge()
# her = Person("Cher")
# her.getLastName()
# plist = [me, her]
# for p in plist: print p
# plist.sort()
# for p in plist: print p
class MITPerson(Person):
nextIdNum = 0 # next ID number to assign
def __init__(self, name):
Person.__init__(self, name) # initialize Person attributes
# new MITPerson attribute: a unique ID number
self.idNum = MITPerson.nextIdNum
MITPerson.nextIdNum += 1
def getIdNum(self):
return self.idNum
# sorting MIT people uses their ID number, not name!
def __lt__(self, other):
return self.idNum < other.idNum
# p1 = MITPerson('Eric')
# p2 = MITPerson('John')
# p3 = MITPerson('John')
# p4 = Person('John')
# print p1
# p1.getIdNum()
# p2.getIdNum()
# p1 < p2
# p3 < p2
# p4 < p1
# p1 < p4
class UG(MITPerson):
def __init__(self, name, classYear):
MITPerson.__init__(self, name)
self.year = classYear
def getClass(self):
return self.year
class Grad(MITPerson):
pass
def isStudent(obj):
return isinstance(obj,UG) or isinstance(obj,Grad)
#s1 = UG('Fred', 2016)
#s2 = Grad('Angela')
#isStudent(s1)
#isStudent(s2)
class TransferStudent(MITPerson):
pass
# go back and define
# class Student(MITPerson)
# change inheritance for UG, Grad and TransferStudent
# change def isStudent(obj):
# return isinstance(obj, Student)
class Grades(object):
"""A mapping from students to a list of grades"""
def __init__(self):
"""Create empty grade book"""
self.students = [] # list of Student objects
self.grades = {} # maps idNum -> list of grades
self.isSorted = True # true if self.students is sorted
def addStudent(self, student):
"""Assumes: student is of type Student
Add student to the grade book"""
if student in self.students:
raise ValueError('Duplicate student')
self.students.append(student)
self.grades[student.getIdNum()] = []
self.isSorted = False
def addGrade(self, student, grade):
"""Assumes: grade is a float
Add grade to the list of grades for student"""
try:
self.grades[student.getIdNum()].append(grade)
except KeyError:
raise ValueError('Student not in grade book')
def getGrades(self, student):
"""Return a list of grades for student"""
try: # return copy of student's grades
return self.grades[student.getIdNum()][:]
except KeyError:
raise ValueError('Student not in grade book')
def allStudents(self):
"""Return a list of the students in the grade book"""
if not self.isSorted:
self.students.sort()
self.isSorted = True
return self.students[:] #return copy of list of students
def gradeReport(course):
"""Assumes: course if of type grades"""
report = []
for s in course.allStudents():
tot = 0.0
numGrades = 0
for g in course.getGrades(s):
tot += g
numGrades += 1
try:
average = tot/numGrades
report.append(str(s) + '\'s mean grade is '
+ str(average))
except ZeroDivisionError:
report.append(str(s) + ' has no grades')
return '\n'.join(report)
ug1 = UG('Jane Doe', 2014)
ug2 = UG('John Doe', 2015)
ug3 = UG('David Henry', 2003)
g1 = Grad('John Henry')
g2 = Grad('George Steinbrenner')
six00 = Grades()
six00.addStudent(g1)
six00.addStudent(ug2)
six00.addStudent(ug1)
six00.addStudent(g2)
for s in six00.allStudents():
six00.addGrade(s, 75)
six00.addGrade(g1, 100)
six00.addGrade(g2, 25)
six00.addStudent(ug3)
#print gradeReport(six00)