Skip to content

Commit b38f6f5

Browse files
committed
Fix bugs and typo errors in closest_pair_of_points.py
1 parent 846bc51 commit b38f6f5

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

DIRECTORY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@
469469

470470
## Geometry
471471
* [Geometry](geometry/geometry.py)
472+
* [Graham Scan](geometry/graham_scan.py)
473+
* [Jarvis March](geometry/jarvis_march.py)
474+
* Tests
475+
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
476+
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
472477

473478
## Graphics
474479
* [Bezier Curve](graphics/bezier_curve.py)

divide_and_conquer/closest_pair_of_points.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@
1818
1919
Time complexity: O(n * log n)
2020
"""
21+
from __future__ import annotations
2122

23+
from typing import Any
2224

23-
def euclidean_distance_sqr(point1, point2):
25+
26+
def euclidean_distance_sqr(point1: list[Any] | tuple[Any, ...], point2: list[Any] | tuple[Any, ...]) -> float:
2427
"""
25-
>>> euclidean_distance_sqr([1,2],[2,4])
28+
>>> euclidean_distance_sqr([1, 2], [2, 4])
2629
5
2730
"""
2831
return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2
2932

3033

31-
def column_based_sort(array, column=0):
34+
def column_based_sort(array: list[Any], column: int = 0) -> list[Any]:
3235
"""
3336
>>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
3437
[(3, 0), (5, 1), (4, 2)]
3538
"""
3639
return sorted(array, key=lambda x: x[column])
3740

3841

39-
def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
42+
def dis_between_closest_pair(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float:
4043
"""
4144
brute force approach to find distance between closest pair points
4245
@@ -46,7 +49,7 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
4649
Returns :
4750
min_dis (float): distance between closest pair of points
4851
49-
>>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
52+
>>> dis_between_closest_pair([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
5053
5
5154
5255
"""
@@ -58,7 +61,7 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
5861
return min_dis
5962

6063

61-
def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")):
64+
def dis_between_closest_in_strip(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float:
6265
"""
6366
closest pair of points in strip
6467
@@ -68,18 +71,18 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")):
6871
Returns :
6972
min_dis (float): distance btw closest pair of points in the strip (< min_dis)
7073
71-
>>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
72-
85
74+
>>> dis_between_closest_in_strip([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
75+
5
7376
"""
7477

75-
for i in range(points_counts-1):
76-
for j in range(i+1, min(i + 6, points_counts)):
78+
for i in range(points_counts - 1):
79+
for j in range(i + 1, min(i + 6, points_counts)):
7780
current_dis = euclidean_distance_sqr(points[i], points[j])
7881
min_dis = min(min_dis, current_dis)
7982
return min_dis
8083

8184

82-
def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_counts):
85+
def closest_pair_of_points_sqr(points_sorted_on_x: list[Any], points_sorted_on_y: list[Any], points_counts: int) -> float:
8386
"""divide and conquer approach
8487
8588
Parameters :
@@ -88,7 +91,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
8891
Returns :
8992
(float): distance btw closest pair of points
9093
91-
>>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6), (7, 8)], 2)
94+
>>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(1, 2), (3, 4)], 2)
9295
8
9396
"""
9497

@@ -122,9 +125,9 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
122125
return min(closest_pair_dis, closest_in_strip)
123126

124127

125-
def closest_pair_of_points(points, points_counts):
128+
def closest_pair_of_points(points: list[Any], points_counts: int) -> float:
126129
"""
127-
>>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)]))
130+
>>> closest_pair_of_points([(2, 3), (12, 30)], 2)
128131
28.792360097775937
129132
"""
130133
points_sorted_on_x = column_based_sort(points, column=0)

0 commit comments

Comments
 (0)