Workspace/Coding labs
Loading progress

Fuse filtered retrieval rankings deterministically

Intermediate60 min

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

  1. Rankings is a sequence of ordered sequences of nonempty string document IDs. Allowed is an iterable of permitted IDs.
  2. Require k to be an exact integer >= 0 and limit an exact integer >= 0; raise ValueError otherwise.
  3. For each list, remove disallowed IDs and repeated occurrences, preserving first occurrence order. Assign ranks from one after filtering.
  4. Each list contributes 1/(k+rank) once per document. Sum contributions across lists.
  5. 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)]

B receives contributions from ranks two and one.
solution.pyPython 3.12