Skip to main content
← CoursesAI Engineering with PythonModule 3 · RAG (Retrieval-Augmented Generation)Hybrid search: BM25 + vectorwrite36 / 101
💬 Discuss🧪 Playground+100 XP
Task
📝 **Question:** **Write the function** \`rrf(rankings, k=60)\` — Reciprocal Rank Fusion. Takes a list of rankings (each ranking is an ordered list of doc IDs from best to worst, from a single retriever) and returns the fused top-K as a list of doc IDs sorted by total RRF score, highest first. Formula for one doc in one ranking at zero-indexed position \`rank\`: \`\`\` score = 1 / (k + rank) \`\`\` Sum scores across all rankings for each doc; sort docs by total score descending. Then fuse a vector and a BM25 retriever for an "SKU-4471" query: \`\`\` vector = ["doc-A", "doc-B", "doc-C", "doc-D"] # vector thinks A is best bm25 = ["doc-D", "doc-A", "doc-E", "doc-F"] # bm25 puts D first (matched the SKU) \`\`\` Print the fused top-4 as a Python list: \`\`\` ['doc-A', 'doc-D', 'doc-B', 'doc-C'] \`\`\` A appears in both lists at high ranks → wins. D was #1 in BM25 → beats B and C even though vector didn't favour it. That's RRF: docs ranked highly by ANY retriever surface. 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.

Keep going

✏️ Write your code here
🐍
Loading Python...
First visit only — ~5-10s. Stays cached afterward.
📊 Result
Press Run to see result...
📣 Help someone learn PythonShare this lesson with a friend — the first 15 are free, no signup.Tweet

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…