Click4Ai

44.

Medium

Write a function angle_between(v1, v2) that computes the angle in radians between two vectors using the cosine similarity formula.

**Formula:** angle = arccos(dot(v1, v2) / (||v1|| * ||v2||))

Example:

Input: v1 = [1, 0], v2 = [0, 1]

Output: 1.5708

**Explanation:** cos(angle) = 0/(1*1) = 0, so angle = arccos(0) = pi/2 ≈ 1.5708

Constraints:

  • Both vectors are non-zero and same length
  • Return angle in radians, rounded to 4 decimal places
  • Result is between 0 and pi
  • Test Cases

    Test Case 1
    Input: [1, 0], [0, 1]
    Expected: 1.5708
    Test Case 2
    Input: [1, 0], [1, 0]
    Expected: 0.0
    Test Case 3
    Input: [1, 0], [-1, 0]
    Expected: 3.1416
    + 2 hidden test cases