#586 · Sorting and searching · Intermediate · Review the AI's code · 60 XP · about 7 min
Someone asked an AI assistant: "Write a function that sorts students by grade, highest first. Students with the same grade should be in alphabetical order." This is its answer. It looks right, but it has a bug.
sort_students(students) takes a list of (name, grade) tuples and returns a new list sorted by grade from highest to lowest. Students with the same grade appear in alphabetical order of their names, A to Z. The input list is not changed.
sort_students([('ben', 70), ('ana', 85), ('cy', 92)]) # returns [('cy', 92), ('ana', 85), ('ben', 70)] sort_students([('zoe', 80), ('amir', 80)]) # returns [('amir', 80), ('zoe', 80)]
students has 0 to 1,000 tuples; names are lowercase, grades are whole numbers from 0 to 100.First solves only, ranked by stars and then time.