Compute the Manhattan distance between two equal-length vectors:
d(x,y)=i=1∑N∣xi−yi∣Here, N is the vector length and xi and yi are corresponding coordinates. Return the distance as a Python float.
Input: x = [1, 2, 3], y = [2, 4, 6]
Output: 6.0
Explanation: The absolute coordinate differences are 1, 2, and 3, which sum to 6.
Input: x = [-1, -2], y = [1, 2]
Output: 6.0
Input: x = [0, 0, 0], y = [0, 0, 0]
Output: 0.0
np.asarray(values, dtype=float) prepares each vector for arithmetic.
np.abs(x - y).sum() computes the L1 distance in one expression.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute the Manhattan distance between two equal-length vectors:
d(x,y)=i=1∑N∣xi−yi∣Here, N is the vector length and xi and yi are corresponding coordinates. Return the distance as a Python float.
Input: x = [1, 2, 3], y = [2, 4, 6]
Output: 6.0
Explanation: The absolute coordinate differences are 1, 2, and 3, which sum to 6.
Input: x = [-1, -2], y = [1, 2]
Output: 6.0
Input: x = [0, 0, 0], y = [0, 0, 0]
Output: 0.0
np.asarray(values, dtype=float) prepares each vector for arithmetic.
np.abs(x - y).sum() computes the L1 distance in one expression.
Sign in to take notes on this problem
Accepts: array
Accepts: array