call(back)
Algorithms & Data StructureseasySign in to star problems

Distance Between Two Points

Return the straight-line (Euclidean) distance between two points. The points have the same number of coordinates, which may be 2 (a plane), 3 (space) or any other dimension.

distance([0, 0], [3, 4])        ->  5
distance([1, 2, 2], [0, 0, 0])  ->  3

Results are compared as numbers, so return the exact floating-point distance.

Hints

Hint 1

In 2-D it's the Pythagorean theorem: sqrt(dx² + dy²). In 3-D add dz². The pattern doesn't stop at three.

Hint 2

Zip the coordinates, sum the squared differences, take one square root — the same line works for any dimension.

Asked at