- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathchebyshev_distance.py
20 lines (17 loc) · 753 Bytes
/
chebyshev_distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
defchebyshev_distance(point_a: list[float], point_b: list[float]) ->float:
"""
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.0, 9.0], [2.0, 2.0, -5.2])
14.2
>>> chebyshev_distance([1.0], [2.0, 2.0])
Traceback (most recent call last):
...
ValueError: Both points must have the same dimension.
"""
iflen(point_a) !=len(point_b):
raiseValueError("Both points must have the same dimension.")
returnmax(abs(a-b) fora, binzip(point_a, point_b))