Normalize a vector to unit length. Essential before cosine similarity — ensures dot product equals cosine similarity.
x_norm[i] = x[i] / ||x||` where `||x|| = sqrt(Σ x[i]²)l2_normalize([3, 4]) → [0.6, 0.8] # magnitude = 5 → 3/5, 4/5
Round to **5 decimal places**.
Similar Problems
Test Cases (2 visible · 2 hidden)
Case 1: 3-4-5 right triangle
Input: l2_normalize([3,4])
Expected: [0.6, 0.8]
Case 2: Already unit vector
Input: l2_normalize([1,0,0])
Expected: [1.0, 0.0, 0.0]
⌘↵ Run · ⌘⇧↵ Submit