PythiloLesson 7 of 8 · progress kept in this browser

Cosine Retrieval

Vector length should not dominate relevance. Normalize dot products by both norms, give zero vectors score zero, and keep stable indices for tied documents.

import math
query, document = [1, 0], [10, 0]
dot = sum(a*b for a,b in zip(query, document))
print(dot / (math.hypot(*query)*math.hypot(*document)))
Your task

rank_vectors(query, documents, k) returns the top k document indices by descending cosine similarity, breaking ties by smaller index. Empty or zero vectors score 0. Dimensions match and k >= 0.