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)
m = Morse()
message = "Hello World"
m.decode( m.encode(message) ) == message.lower()
True
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
students = ["Charlie", "James", "Matt"]
exams = { "maths" : 20, "physics" : 50, "english literature" : 30 }
results = { "maths" : { "James" : 20, "Matt" : 18, "Charlie" : 4 },
"physics" : { "Matt" : 48, "James" : 3 },
"english literature" : { "Charlie" : 10, "Matt" : 25, "James" : 3 } }
school = School()
for student in students:
school.addStudent(student)
for exam in exams.keys():
school.addExam(exam, exams[exam])
for exam in results:
add_results(school, exam, results[exam])
school.grades()
{'Charlie': {'maths': 'F', 'physics': 'U', 'english literature': 'F'}, 'James': {'maths': 'A', 'physics': 'F', 'english literature': 'F'}, 'Matt': {'maths': 'B', 'physics': 'A', 'english literature': 'B'}}
school.resits()
{'Charlie': ['maths', 'physics', 'english literature'], 'James': ['physics', 'english literature']}
school.prizeStudent()
'Matt'
school.reviseCourse(50)
{'physics': 34.0, 'english literature': 42.22222222222222}