Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-406

Retrieval Precision at K

Problem

Precision at k is the number of relevant documents among the top k divided by k. Return 0.0 when k <= 0.

Given the ranked retrieved ids, the set of relevant ids, and k, return precision at k.

Hint

Denominator is k even when fewer than k documents were retrieved.

Test cases

case 1
in: [["a","b","c"],["a","c"],2]
expected: 0.5
case 2
in: [["x"],["y"],1]
expected: 0
case 3
in: [["a","b"],["b"],2]
expected: 0.5
Starter codePython
def retrieval_precision_at_k(retrieved, relevant, k):
    # Your code here
    pass

3 test cases · run your code against them in the editor