Click4Ai

36.

Easy

Write a function manhattan_distance(p1, p2) that computes the Manhattan (L1 / taxicab) distance between two points. This is the sum of absolute differences along each dimension.

**Formula:** d = sum(|p1[i] - p2[i]|)

Example:

Input: p1 = [0, 0], p2 = [3, 4]

Output: 7

**Explanation:** |3-0| + |4-0| = 3 + 4 = 7

Constraints:

  • Both points have the same dimensionality
  • Return an integer or float
  • Test Cases

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