Fuse filtered retrieval rankings deterministically
Implement fuse_rankings(rankings, allowed, k=60, limit=10). Filter inaccessible IDs and duplicates within each list, assign compact ranks, then combine reciprocal-rank contributions.
Your task
- Rankings is a sequence of ordered sequences of nonempty string document IDs. Allowed is an iterable of permitted IDs.
- Require k to be an exact integer >= 0 and limit an exact integer >= 0; raise ValueError otherwise.
- For each list, remove disallowed IDs and repeated occurrences, preserving first occurrence order. Assign ranks from one after filtering.
- Each list contributes 1/(k+rank) once per document. Sum contributions across lists.
- Return at most limit (document_id, score) tuples, sorted by descending score and ascending ID for ties. Empty inputs or zero limit return an empty list. Do not mutate inputs.
Examples
EXAMPLE 1
Input[['A','B'],['B','C']], k=0, all allowed
Output[('B',1.5),('A',1.0),('C',0.5)]
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor