Advanced
Pick diverse results with MMR
Write mmr(query, vectors, lam, k). vectors is a dict from name to vector (a list of numbers). cosine(a, b) is given in the starter.
Pick k names one at a time. In each round score every unpicked name as lam * cosine(query, vector) - (1 - lam) * redundancy, where redundancy is the highest cosine between that name's vector and any already picked vector (0.0 if none are picked). Take the highest score; on a tie take the name that comes first in the dict. Return the picked names in order.
What Check tests
- lambda 1 is plain top-k by relevance
- A lower lambda swaps a copy for a different passage
- k larger than the number of names returns all of them
- Ties take the name that comes first
- The first pick ignores redundancy
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.