Gaps in the Grid

Data with PythonMedium+10 XP+10

Gaps in the Grid

A school records test results: the class, the subject and the score. The head wants a grid of average scores, one row per class and one column per subject.

Some classes haven't sat a test in some subjects yet. Writing 0 in those boxes would look like the class scored zero, which is wrong. Those boxes must stay empty.

Task: write score_grid(classes, subjects, scores). Result i is class classes[i] scoring scores[i] in subjects[i].

Return a dictionary with two keys:

  • "subjects": every subject that appears, sorted alphabetically.
  • "rows": one list per class, sorted alphabetically by class. Each list is the class name followed by its average score in each subject, in the same order as "subjects".

Each average is a float rounded to 1 decimal place. A box with no results is None. There is at least one result.

Examples

classes = ["7A", "7B", "7A", "7B", "7A", "7C"]
subjects = ["maths", "maths", "art", "maths", "maths", "art"]
scores = [60, 70, 80, 75, 64, 90]
returns {'subjects': ['art', 'maths'], 'rows': [['7A', 80.0, 62.0], ['7B', None, 72.5], ['7C', 90.0, None]]}
classes = ["9X", "9X", "9Y"]
subjects = ["french", "french", "french"]
scores = [55, 62, 71]
returns {'subjects': ['french'], 'rows': [['9X', 58.5], ['9Y', 71.0]]}

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run