Editor

Class Gradebook

#628 · Working with data · Intermediate · Mini-project · 160 XP · about 60 min

Build the gradebook a teacher uses all term. The gradebook is a dictionary that maps each student's name to the list of their marks, in the order the marks were added, for example {'Ava': [91, 78], 'Ben': [64]}. Every function takes the gradebook as its first argument, and each part can be tested on its own.

Parts

  • Part 1 add_mark(book, name, mark): add mark to the end of the student's list, creating the student if they are new, and return True. A mark must be a whole number (an int) from 0 to 100: anything else is not added, and the function returns False.
  • Part 2 average(book, name): the student's average mark, rounded with round(..., 1). A student with no marks, or who is not in the book, has an average of 0.0.
  • Part 3 letter_grade(avg): the letter for an average: 'A' for 90 and above, 'B' for 80 and above, 'C' for 70 and above, 'D' for 60 and above, and 'F' below 60.
  • Part 4 report(book): a list of (name, average, letter) tuples, one for each student, in alphabetical order of name.
  • Part 5 top_students(book, n): the names of the n students with the highest averages (as Part 2 works them out), best first. Students with equal averages go in alphabetical order, and if there are fewer than n students, return them all.

Examples

book = {}
add_mark(book, 'Ava', 91)    # returns True
add_mark(book, 'Ava', 78)    # returns True
add_mark(book, 'Ben', 64)    # returns True
add_mark(book, 'Ben', 105)   # returns False, so book is {'Ava': [91, 78], 'Ben': [64]}

average(book, 'Ava')         # returns 84.5
letter_grade(84.5)           # returns 'B'
report(book)                 # returns [('Ava', 84.5, 'B'), ('Ben', 64.0, 'D')]
top_students(book, 1)        # returns ['Ava']

Constraints

  • A gradebook has at most 500 students, each with at most 200 marks.
  • Names are non-empty strings that start with a capital letter.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.