RAG and Vector Search · Exercise 5 of 5 Previous
Intermediate
Widen a filtered search until k results pass
ranked is a list of (id, owner) already sorted best first. Write keep_widening(ranked, wanted, k, start).
It looks at the first size items (starting at size = start), keeps the ids whose owner is wanted (at most k, in order), and returns (ids, size). If fewer than k were found and size is less than len(ranked), it doubles size and tries again. It stops when k ids are found or all items were looked at, and returns the size it last used.
What Check tests
- Widens 2, 4, 8 until three x items are found
- Stops early when the first window is enough
- Returns what exists when k can never be reached
- Never returns more than k
- The window never needs to grow for an unknown owner beyond the data
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.