Command Palette

Search for a command to run...

Hectal
Phase 5: Vector Databases
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

  1. Widens 2, 4, 8 until three x items are found
  2. Stops early when the first window is enough
  3. Returns what exists when k can never be reached
  4. Never returns more than k
  5. 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.