All problems
Retrieval Precision at k
Problem
Return precision at k: the fraction of the first k retrieved ids that are relevant.
k is capped by the retrieved list length. Return 0.0 when k <= 0 or the retrieved list is empty.
Hint
Use the actual cut length in the denominator, not k itself.
Test cases
case 1
in: [["a","b","c","d"],["a","c"],2]
expected: 0.5
case 2
in: [["a","b"],["a"],1]
expected: 1
case 3
in: [[],["a"],3]
expected: 0
case 4
in: [["a"],["b"],0]
expected: 0
case 5
in: [["a","b"],["a","b"],5]
expected: 1
Starter codePython
def precision_at_k(retrieved, relevant, k):
# Your code here
pass5 test cases · run your code against them in the editor