Assign 1-based ranks to a list of scores: the highest score gets rank 1, the next gets rank 2, and so on. The sort step is treated as a known primitive; the replay focuses on ranks and rank being updated one entry at a time.

By hand

The Pythonic way

zip(scores, names) pairs each score with its name; sorted(..., reverse=True) orders by score descending; enumerate supplies the 0-based position, giving i + 1 as the 1-based rank. A final {k: ranks[k] for k in sorted(ranks)} returns the dict sorted by name.

naive.py
names = ['amy', 'bob', 'cal', 'dan', 'eve', 'fay']
scores = [72, 88, 55, 93, 61, 78]
order = sorted(range(len(names)), key=lambda i: scores[i], reverse=True)
ranks = {}
rank = 1
for i in order:
    ranks[names[i]] = rank
    rank = rank + 1
print('RESULT:', {k: ranks[k] for k in sorted(ranks)})
library.py
names = ['amy', 'bob', 'cal', 'dan', 'eve', 'fay']
scores = [72, 88, 55, 93, 61, 78]
ranks = {name: i + 1 for i, (s, name) in enumerate(sorted(zip(scores, names), reverse=True))}
result = {k: ranks[k] for k in sorted(ranks)}
print('RESULT:', result)
RESULT: {'amy': 4, 'bob': 2, 'cal': 6, 'dan': 1, 'eve': 5, 'fay': 3}

Implementation notes

  • ranks grows visibly in the replay: {}{'dan': 1}{'dan': 1, 'bob': 2} → … → the full 6-entry dict (60 chars, within the 80-char limit).
  • Ties are out of scope: with distinct scores, every rank is unique. Handling ties requires dense/standard/min-rank strategies (see scipy.stats.rankdata).
  • {k: ranks[k] for k in sorted(ranks)} sorts the output dict by name for a deterministic, readable RESULT line.