Editor

Average Mark per Student

#237 · Working with data · Beginner · 35 XP · about 6 min

A teacher records every mark of the term in one list of (name, score) pairs, in the order the work was handed in, so each student's marks are scattered through the list. Write student_averages(scores) that returns a dictionary mapping each student's name to their average: the total of their scores divided by how many there are, rounded to one decimal place with round(total / count, 1). Averages are floats, so an average of exactly 72 is 72.0.

Put the names in the dictionary in the order each student first appears in scores. An empty list gives an empty dictionary.

Examples

student_averages([('Mia', 80), ('Leo', 72), ('Mia', 95)])
# returns {'Mia': 87.5, 'Leo': 72.0}
student_averages([('Ava', 70), ('Ava', 71), ('Ava', 74)])
# returns {'Ava': 71.7}

Ava's marks add up to 215, and 215 / 3 = 71.666..., which rounds to 71.7.

Constraints

  • 0 <= len(scores) <= 10000
  • Every score is a whole number from 0 to 100.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.