MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / chebyshev_distance

Function chebyshev_distance

maths/chebyshev_distance.py:1–20  ·  view source on GitHub ↗

This function calculates the Chebyshev distance (also known as the Chessboard distance) between two n-dimensional points represented as lists. https://en.wikipedia.org/wiki/Chebyshev_distance >>> chebyshev_distance([1.0, 1.0], [2.0, 2.0]) 1.0 >>> chebyshev_distance([1.0, 1

(point_a: list[float], point_b: list[float])

Source from the content-addressed store, hash-verified

1def chebyshev_distance(point_a: list[float], point_b: list[float]) -> float:
2 """
3 This function calculates the Chebyshev distance (also known as the
4 Chessboard distance) between two n-dimensional points represented as lists.
5
6 https://en.wikipedia.org/wiki/Chebyshev_distance
7
8 >>> chebyshev_distance([1.0, 1.0], [2.0, 2.0])
9 1.0
10 >>> chebyshev_distance([1.0, 1.0, 9.0], [2.0, 2.0, -5.2])
11 14.2
12 >>> chebyshev_distance([1.0], [2.0, 2.0])
13 Traceback (most recent call last):
14 ...
15 ValueError: Both points must have the same dimension.
16 """
17 if len(point_a) != len(point_b):
18 raise ValueError("Both points must have the same dimension.")
19
20 return max(abs(a - b) for a, b in zip(point_a, point_b))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected