RAG and Vector Search · Exercise 6 of 6 Previous
Intermediate
Filtering after the search
Write fetch_needed(k, matching, total) that returns how many results to fetch before filtering so that about k of them pass, rounded up: ceil(k * total / matching) with integer arithmetic only.
ranked is the list of all ids, closest first, and allowed is the set of ids that pass the filter. Write post_filter(ranked, allowed, k, fetch) that looks only at the first fetch ids of ranked and returns the first k of them that are in allowed, in order. It may return fewer than k.
What Check tests
- 5 percent selectivity needs a fetch of 100 for k = 5
- The fetch rounds up
- A filter that passes everything needs only k
- A small fetch can return too few
- A bigger fetch finds k
- At most k results, in ranked order
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.