RAG and Vector Search · Exercise 1 of 5Next
Beginner
Top-k search by dot product
vectors is a dictionary from id to a vector (a list of numbers). Write top_k(vectors, query, k) that returns the ids of the k vectors with the largest dot product with query, best first.
Equal scores keep the dictionary's order. If there are fewer than k vectors, return them all.
What Check tests
- The best two come back, best first
- A clear winner comes first
- k larger than the data returns everything
- An empty store returns an empty list
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.