All problems
Top-K Class Indices
Problem
Return the indices of the k largest scores in descending order.
Ties are broken by the smaller index. k may be 0 (returns []) or larger than the score list (returns all indices).
Hint
Sort the indices by (-score, index) and slice the first k.
Test cases
case 1
in: [[0.1,0.9,0.5,0.9],2]
expected: [1,3]
case 2
in: [[0.1,0.9,0.5,0.9],1]
expected: [1]
case 3
in: [[0.1,0.9,0.5,0.9],4]
expected: [1,3,2,0]
case 4
in: [[0.1,0.9,0.5,0.9],0]
expected: []
Starter codePython
def top_k_indices(scores, k):
# Your code here
pass4 test cases · run your code against them in the editor