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] pairs

  • Return 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
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.