Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-053

Vector Normalization

Problem

Return the unit vector in the direction of v: v / ||v||_2.

If v is the zero vector, return a list of zeros with the same length.

Hint

Divide each component by the L2 norm.

Test cases

case 1
in: [[3,4]]
expected: [0.6,0.8]
case 2
in: [[1,0,0]]
expected: [1,0,0]
case 3
in: [[0,0]]
expected: [0,0]
case 4
in: [[1,1,1]]
expected: [0.5773502691896258,0.5773502691896258,0.5773502691896258]
Starter codePython
def normalize(v):
    # Your code here
    pass

4 test cases · run your code against them in the editor