Click4Ai

43.

Medium

Write a function project(a, b) that computes the projection of vector a onto vector b.

**Formula:** proj_b(a) = (dot(a, b) / dot(b, b)) * b

Example:

Input: a = [3, 4], b = [1, 0]

Output: [3.0, 0.0]

**Explanation:** dot(a,b)=3, dot(b,b)=1, scalar=3, projection = 3*[1,0] = [3,0]

Constraints:

  • Both vectors have the same length
  • b is non-zero
  • Return a list of floats rounded to 4 decimal places
  • Test Cases

    Test Case 1
    Input: [3, 4], [1, 0]
    Expected: [3.0, 0.0]
    Test Case 2
    Input: [1, 2], [1, 1]
    Expected: [1.5, 1.5]
    Test Case 3
    Input: [5, 0], [0, 3]
    Expected: [0.0, 0.0]
    + 2 hidden test cases