summaryrefslogtreecommitdiff
path: root/mit/extented_class_example/GradeBook.py
diff options
context:
space:
mode:
Diffstat (limited to 'mit/extented_class_example/GradeBook.py')
-rw-r--r--mit/extented_class_example/GradeBook.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/mit/extented_class_example/GradeBook.py b/mit/extented_class_example/GradeBook.py
new file mode 100644
index 0000000..36c25fb
--- /dev/null
+++ b/mit/extented_class_example/GradeBook.py
@@ -0,0 +1,94 @@
+#!/usr/bin/python3
+
+from MITPerson import *
+
+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('Duplicated student')
+
+ self.students.append(student)
+ self.grades[student.getIdNum()] = [] # dict is a ID:List pair
+ 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 a 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 a COPY of students, so we avoid any
+ # externalmodification to the original list
+
+
+
+### TESTS
+
+def gradeReport(course):
+ """ Assumes: course is 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:
+ mean = tot/numGrades
+ report.append(str(s) + '\'s mean grade is ' + str(mean))
+ except ZeroDivisionError:
+ report.append(str(s) + ' has no grades')
+
+ return ' \n'.join(report)
+
+
+six00 = Grades()
+ug3 = UG('Drew Houston', 2017)
+ug1 = UG('Matt Damon', 2018)
+ug2 = UG('Ben Affleck', 2019)
+ug4 = UG('Mark Zuckerberg', 2017)
+g1 = Grad('Bill Gates')
+g2 = Grad('Steve Wozniak')
+
+six00.addStudent(g1)
+six00.addStudent(ug2)
+six00.addStudent(ug1)
+six00.addStudent(g2)
+six00.addStudent(ug4)
+six00.addStudent(ug3)
+
+six00.addGrade(g1, 90)
+six00.addGrade(g2, 45)
+six00.addGrade(ug1, 80)
+six00.addGrade(ug2, 75)