Save week07.py in your exercise folder. Run python week07.py --lesson 1. The script needs only Python’s standard library, no account, download, or graphics processor. Its tiny corpus and handcrafted feature encoder use negligible memory and should finish in seconds on a normal processor. This is a checked baseline for retrieval mechanics; an optional local embedding endpoint can replace the encoder in a separately requested run.
The script imports math for square roots. zip(a, b) pairs matching coordinates; sum(...) adds the generated products. A conditional checks zero length before division. The core computation is:
import math
def cosine(a, b):
assert len(a) == len(b)
dot = sum(x * y for x, y in zip(a, b))
norm = math.sqrt(sum(x*x for x in a) * sum(y*y for y in b))
return dot / norm if norm else 0.0
assert cosine([1, 0], [0, 1]) == 0.0
Read the printed query rankings and PASS lines. Matching dimensions, zero-vector handling, and source-slice equality should pass. The exact-code query deliberately exposes a dense-representation tie. A passing assertion verifies its stated invariant; it does not establish general semantic understanding.