Write a function unit_vector(v) that normalizes a vector to unit length (magnitude = 1). Divide each element by the vector's magnitude.
**Formula:** u = v / ||v||
Example:
Input: v = [3, 4]
Output: [0.6, 0.8]
**Explanation:** magnitude = 5.0, so [3/5, 4/5] = [0.6, 0.8]
Constraints:
Test Cases
Test Case 1
Input:
[3, 4]Expected:
[0.6, 0.8]Test Case 2
Input:
[1, 0, 0]Expected:
[1.0, 0.0, 0.0]Test Case 3
Input:
[1, 1]Expected:
[0.7071, 0.7071]+ 2 hidden test cases