Functions as Arguments
~10 mincode completion
Write sort_by_score(records) that sorts a list of (name, score) pairs from highest score to lowest and returns the sorted list.
Examples
Highest score comes first
- Input
- sort_by_score([["ada", 70], ["grace", 95], ["alan", 82]])
- Output
- [["grace", 95], ["alan", 82], ["ada", 70]]
An already sorted list is left in order
- Input
- sort_by_score([["a", 9], ["b", 5]])
- Output
- [["a", 9], ["b", 5]]
A single record comes back unchanged
- Input
- sort_by_score([["solo", 1]])
- Output
- [["solo", 1]]
Hints
Hint 1
Sorting first makes the rest straightforward.
Hint 2
Watch for this: sorted ascending.
Requirements
records: a list of [name, score] pairsReturn the same pairs, ordered from highest score to lowest.
Constraints
Standard library only, no imports required
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~10 min
••••••
3 employers weight this skill
2 frontier labs, 1 AI product company. Top match scores 80.
Python
def sort_by_score(records):
"""
Sort (name, score) pairs by score, highest first.
Args:
records: a list of [name, score] pairs
Returns:
The same pairs, ordered from highest score to lowest.
"""
# YOUR CODE HERE
pass