Euclidean Distance: Pythagoras in Any Dimension

Linear AlgebraEasy+10 XP+10

Euclidean Distance: Pythagoras in Any Dimension

Euclidean distance is straight-line distance — Pythagoras extended to as many dimensions as you like:

1d = sqrt(Σ (a[i] - b[i])²)

Task: write euclidean_distance(point_a, point_b) returning the distance, rounded to 4 decimal places.

  • The points always have the same number of dimensions.
  • Two identical points are 0 apart.

It's the default distance almost everywhere — k-NN, k-means, nearest-neighbour search — but it gets unhelpful in very high dimensions, where all pairwise distances drift toward each other and "nearest" stops meaning much. That's why embeddings are usually compared by cosine instead.

Bird's Flight vs. Taxi Route

Two ways to measure the distance between the same two points on a city grid.

AB
A bird flies straight from A to B: distance = 7.62 blocks. This is Euclidean distance.

Examples

point_a = [0.0, 0.0]
point_b = [3.0, 4.0]
returns 5.0
point_a = [1.0, 1.0, 1.0]
point_b = [2.0, 2.0, 2.0]
returns 1.7321

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run