Beginner
Rank vectors by cosine similarity
DOCS maps names to vectors (they do not have to be length 1).
Write cosine(a, b): the dot product divided by both lengths. If either vector has length 0, return 0.0.
Write top_k(query, docs, k) returning a list of (name, score) pairs, best first, at most k of them. Ties keep the order of docs.
What Check tests
cosineignores vector length- A zero vector scores 0
top_kreturns the best first, with scores- Ties keep the order of the documents
klarger than the number of documents is fine
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.