Functions and Classes in Python

Solutions

In [27]:
class Morse:
    def __init__(self):
        self._letter_to_morse = {'a':'.-', 'b':'-...', 'c':'-.-.', 'd':'-..', 'e':'.', 'f':'..-.',
                   'g':'--.', 'h':'....', 'i':'..', 'j':'.---', 'k':'-.-', 'l':'.-..', 'm':'--',
                   'n':'-.', 'o':'---', 'p':'.--.', 'q':'--.-', 'r':'.-.', 's':'...', 't':'-',
                   'u':'..-', 'v':'...-', 'w':'.--', 'x':'-..-', 'y':'-.--', 'z':'--..',
                   '0':'-----', '1':'.----', '2':'..---', '3':'...--', '4':'....-',
                   '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.',
                   ' ':'/' }
        self._morse_to_letter = {}
        for letter in self._letter_to_morse.keys():
            self._morse_to_letter[ self._letter_to_morse[letter] ] = letter
    def encode(self, message):
        morse = []
        for letter in message:
            morse.append( self._letter_to_morse[letter.lower()] )
        return morse
    def decode(self, morse):
        message = []
        for code in morse:
            message.append( self._morse_to_letter[code] )
        return "".join(message)
In [28]:
m = Morse()
In [29]:
message = "Hello World"
In [30]:
m.decode( m.encode(message) ) == message.lower()
Out[30]:
True

In [31]:
class School:
    def __init__(self):
        self._students = {}
        self._exams = []
    def addStudent(self, name):
        self._students[name] = Student()
    def addExam(self, exam, max_score):
        self._exams.append(exam)
        for key in self._students.keys():
            self._students[key].addExam(exam, Exam(max_score))
    def addResult(self, name, exam, score):
        self._students[name].addResult(exam, score)
    def grades(self):
        g = {}
        for name in self._students.keys():
            g[name] = self._students[name].grades()
        return g
    def resits(self):
        r = {}
        for name in self._students.keys():
            student_resits = []
            for exam in self._exams:
                grade = self._students[name].grade(exam)
                if (grade == "F" or grade == "U"):
                    student_resits.append(exam)
                if len(student_resits) > 0:
                    r[name] = student_resits
        return r
    def prizeStudent(self):
        prize_score = 0
        prize_student = None
        for name in self._students.keys():
            avg_score = 0
            for exam in self._exams:
                avg_score += self._students[name].result(exam)
            avg_score /= len(self._exams)
            if avg_score > prize_score:
                prize_score = avg_score
                prize_student = name
        return prize_student
    def reviseCourse(self, threshold=50):
        revise_course = {}
        for exam in self._exams:
            avg_score = 0
            for name in self._students.keys():
                avg_score += self._students[name].result(exam)
            avg_score /= len(self._students)
            if avg_score < threshold:
                revise_course[exam] = avg_score
        return revise_course
In [32]:
students = ["Charlie", "James", "Matt"]
In [33]:
exams = { "maths" : 20, "physics" : 50, "english literature" : 30 }
In [34]:
results = { "maths" : { "James" : 20, "Matt" : 18, "Charlie" : 4 }, 
            "physics" : { "Matt" : 48, "James" : 3 },
            "english literature" : { "Charlie" : 10, "Matt" : 25, "James" : 3 } }
In [35]:
school = School()
In [36]:
for student in students:
    school.addStudent(student)
In [37]:
for exam in exams.keys():
    school.addExam(exam, exams[exam])
In [38]:
for exam in results:
    add_results(school, exam, results[exam])
In [39]:
school.grades()
Out[39]:
{'Charlie': {'maths': 'F', 'physics': 'U', 'english literature': 'F'},
 'James': {'maths': 'A', 'physics': 'F', 'english literature': 'F'},
 'Matt': {'maths': 'B', 'physics': 'A', 'english literature': 'B'}}
In [40]:
school.resits()
Out[40]:
{'Charlie': ['maths', 'physics', 'english literature'],
 'James': ['physics', 'english literature']}
In [41]:
school.prizeStudent()
Out[41]:
'Matt'
In [42]:
school.reviseCourse(50)
Out[42]:
{'physics': 34.0, 'english literature': 42.22222222222222}