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

Implement Euclidean Distance

Linear Algebra
Easy

Compute the Euclidean distance between two equal-length vectors:

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

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 = [3, 4], y = [0, 0]

Output: 5.0

Explanation: The squared differences are 9 and 16, so the distance is the square root of 25.

Input: x = [1, 2, 3], y = [4, 5, 6]

Output: 5.196152

Input: x = [0, 0, 0], y = [0, 0, 0]

Output: 0.0

Hint 1

difference = np.asarray(x, dtype=float) - np.asarray(y, dtype=float) forms the displacement vector.

Hint 2

np.sqrt(np.sum(difference ** 2)) computes its L2 norm.

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
Manhattan DistanceCosine SimilarityDot ProductKnn DistanceK Means Assignment

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 Euclidean Distance

Linear Algebra
Easy

Compute the Euclidean distance between two equal-length vectors:

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

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 = [3, 4], y = [0, 0]

Output: 5.0

Explanation: The squared differences are 9 and 16, so the distance is the square root of 25.

Input: x = [1, 2, 3], y = [4, 5, 6]

Output: 5.196152

Input: x = [0, 0, 0], y = [0, 0, 0]

Output: 0.0

Hint 1

difference = np.asarray(x, dtype=float) - np.asarray(y, dtype=float) forms the displacement vector.

Hint 2

np.sqrt(np.sum(difference ** 2)) computes its L2 norm.

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
Manhattan DistanceCosine SimilarityDot ProductKnn DistanceK Means Assignment

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

You must run your code first.