Click4Ai

32.

Easy

Write a function magnitude(v) that computes the magnitude (L2 norm / Euclidean norm) of a vector without using np.linalg.norm().

**Formula:** ||v|| = sqrt(v[0]^2 + v[1]^2 + ... + v[n]^2)

Example:

Input: v = [3, 4]

Output: 5.0

**Explanation:** sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5.0

Constraints:

  • 1 <= len(v) <= 1000
  • Return a float
  • Test Cases

    Test Case 1
    Input: [3, 4]
    Expected: 5.0
    Test Case 2
    Input: [1, 0, 0]
    Expected: 1.0
    Test Case 3
    Input: [1, 1]
    Expected: 1.4142135623730951
    + 2 hidden test cases