Click4Ai

42.

Medium

Write a function cross_product(v1, v2) that computes the cross product of two 3D vectors without using np.cross().

**Formula:** v1 × v2 = [v1[1]*v2[2] - v1[2]*v2[1],

v1[2]*v2[0] - v1[0]*v2[2],

v1[0]*v2[1] - v1[1]*v2[0]]

Example:

Input: v1 = [1, 2, 3], v2 = [4, 5, 6]

Output: [-3, 6, -3]

Constraints:

  • Both vectors have exactly 3 elements
  • Return a list of 3 values
  • The cross product is only defined in 3D
  • Test Cases

    Test Case 1
    Input: [1, 2, 3], [4, 5, 6]
    Expected: [-3, 6, -3]
    Test Case 2
    Input: [1, 0, 0], [0, 1, 0]
    Expected: [0, 0, 1]
    Test Case 3
    Input: [2, 3, 4], [5, 6, 7]
    Expected: [-3, 6, -3]
    + 2 hidden test cases