#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.
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.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.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.report(book): a list of (name, average, letter) tuples, one for each student, in alphabetical order of name.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.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']
First solves only, ranked by stars and then time.