TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Manhattan Distance

Linear Algebra
Easy

Compute the Manhattan distance between two equal-length vectors:

d(x,y)=∑i=1N∣xi−yi∣d(x,y) = \sum_{i=1}^{N} \lvert x_i - y_i \rvertd(x,y)=i=1∑N​∣xi​−yi​∣

Here, NNN is the vector length and xix_ixi​ and yiy_iyi​ are corresponding coordinates. Return the distance as a Python float.

Loading visualization...

Examples

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

Hint 1

np.asarray(values, dtype=float) prepares each vector for arithmetic.

Hint 2

np.abs(x - y).sum() computes the L1 distance in one expression.

Requirements

  • Convert both input lists to NumPy arrays
  • Compute all coordinate differences without a Python element loop
  • Return a Python float

Constraints

  • x and y are nonempty one-dimensional lists with the same length
  • Use NumPy only
Try Similar Problems
Euclidean DistanceCosine SimilarityDot ProductKnn DistanceMatrix Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Implement Manhattan Distance

Linear Algebra
Easy

Compute the Manhattan distance between two equal-length vectors:

d(x,y)=∑i=1N∣xi−yi∣d(x,y) = \sum_{i=1}^{N} \lvert x_i - y_i \rvertd(x,y)=i=1∑N​∣xi​−yi​∣

Here, NNN is the vector length and xix_ixi​ and yiy_iyi​ are corresponding coordinates. Return the distance as a Python float.

Loading visualization...

Examples

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

Hint 1

np.asarray(values, dtype=float) prepares each vector for arithmetic.

Hint 2

np.abs(x - y).sum() computes the L1 distance in one expression.

Requirements

  • Convert both input lists to NumPy arrays
  • Compute all coordinate differences without a Python element loop
  • Return a Python float

Constraints

  • x and y are nonempty one-dimensional lists with the same length
  • Use NumPy only
Try Similar Problems
Euclidean DistanceCosine SimilarityDot ProductKnn DistanceMatrix Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.