Skip to content

Commit 70a5e76

Browse files
keonclaude
andcommitted
Export chebyshev_distance from algorithms.math and polish module
- Register chebyshev_distance in algorithms/math/__init__.py (import + __all__) so `from algorithms.math import chebyshev_distance` resolves to the function, not the submodule. Without this, the test in test_math.py fails with TypeError ('module' is not callable). - Align the module style with manhattan_distance.py: module docstring, `from __future__ import annotations`, type hints, doctest examples, and the project's ValueError raising convention. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ffc6979 commit 70a5e76

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

algorithms/math/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
modular_inverse, # type: ignore[no-redef] # noqa: E402
1313
)
1414
from algorithms.math.base_conversion import base_to_int, int_to_base
15+
from algorithms.math.chebyshev_distance import chebyshev_distance
1516
from algorithms.math.combination import combination, combination_memo
1617
from algorithms.math.cosine_similarity import cosine_similarity
1718
from algorithms.math.decimal_to_binary_ip import (
@@ -66,6 +67,7 @@
6667
__all__ = [
6768
"base_to_int",
6869
"int_to_base",
70+
"chebyshev_distance",
6971
"chinese_remainder_theorem",
7072
"combination",
7173
"combination_memo",
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
def chebyshev_distance(point1, point2):
2-
"""
3-
Calculate the Chebyshev distance between two points.
1+
"""Chebyshev distance — L-infinity distance between two points.
2+
3+
Also known as chessboard distance, it is the maximum absolute difference
4+
across coordinates. In chess, it is the minimum number of moves a king
5+
needs to travel between two squares.
6+
"""
7+
8+
from __future__ import annotations
9+
410

5-
The Chebyshev distance is defined as the maximum absolute difference
6-
between the coordinates of the points.
11+
def chebyshev_distance(a: tuple[float, ...], b: tuple[float, ...]) -> float:
12+
"""Return the Chebyshev (L-infinity) distance between points *a* and *b*.
713
8-
Parameters:
9-
point1 (list or tuple): The first point as a list or tuple of coordinates.
10-
point2 (list or tuple): The second point as a list or tuple of coordinates.
14+
Works in any number of dimensions.
1115
12-
Returns:
13-
float: The Chebyshev distance between the two points.
16+
>>> chebyshev_distance((1, 2, 3), (4, 5, 6))
17+
3
18+
>>> chebyshev_distance((1, 2), (1, 2))
19+
0
20+
>>> chebyshev_distance((-1, -2), (4, 3))
21+
5
1422
"""
15-
if len(point1) != len(point2):
16-
raise ValueError("Points must have the same number of dimensions.")
17-
18-
return max(abs(a - b) for a, b in zip(point1, point2))
23+
if len(a) != len(b):
24+
msg = "Points must have the same number of dimensions."
25+
raise ValueError(msg)
26+
return max(abs(x - y) for x, y in zip(a, b, strict=False))

0 commit comments

Comments
 (0)