from collections import defaultdict
class WeightedGradebook:
def __init__(self):
self._grades = {}
def add_student(self, name):
self._grades[name] = defaultdict(list)
def report_grade(self, name, subject, score, weight):
subjects = self._grades[name]
grades_for_subject = subjects[subject]
grades_for_subject.append((score, weight))
def average_grade(self, name):
subjects = self._grades[name]
score_sum, score_count = 0, 0
for scores in subjects.values():
subject_avg, total_weight = 0, 0
for score, weight in scores:
subject_avg += score * weight
total_weight += weight
score_sum += subject_avg / total_weight
score_count += 1
return score_sum / score_count
book = WeightedGradebook()
book.add_student("Issac Newton")
book.report_grade("Issac Newton", "Math", 75, 0.05)
book.report_grade("Issac Newton", "Math", 65, 0.15)
book.report_grade("Issac Newton", "Math", 70, 0.80)
book.report_grade("Issac Newton", "Gym", 100, 0.40)
book.report_grade("Issac Newton", "Gym", 85, 0.60)
print(book.average_grade("Issac Newton"))