1919Time complexity: O(n * log n)
2020"""
2121
22+ from __future__ import annotations
2223
23- def euclidean_distance_sqr (point1 , point2 ):
24+ from typing import Any
25+
26+
27+ def euclidean_distance_sqr (
28+ point1 : list [Any ] | tuple [Any , ...], point2 : list [Any ] | tuple [Any , ...]
29+ ) -> float :
2430 """
25- >>> euclidean_distance_sqr([1,2],[2,4])
31+ >>> euclidean_distance_sqr([1, 2], [2, 4])
2632 5
2733 """
2834 return (point1 [0 ] - point2 [0 ]) ** 2 + (point1 [1 ] - point2 [1 ]) ** 2
2935
3036
31- def column_based_sort (array , column = 0 ) :
37+ def column_based_sort (array : list [ Any ] , column : int = 0 ) -> list [ Any ] :
3238 """
3339 >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
3440 [(3, 0), (5, 1), (4, 2)]
3541 """
3642 return sorted (array , key = lambda x : x [column ])
3743
3844
39- def dis_between_closest_pair (points , points_counts , min_dis = float ("inf" )):
45+ def dis_between_closest_pair (
46+ points : list [Any ], points_counts : int , min_dis : float | None = None
47+ ) -> float :
4048 """
4149 brute force approach to find distance between closest pair points
4250
@@ -46,10 +54,12 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
4654 Returns :
4755 min_dis (float): distance between closest pair of points
4856
49- >>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
57+ >>> dis_between_closest_pair([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
5058 5
5159
5260 """
61+ if min_dis is None :
62+ min_dis = float ("inf" )
5363
5464 for i in range (points_counts - 1 ):
5565 for j in range (i + 1 , points_counts ):
@@ -58,7 +68,9 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
5868 return min_dis
5969
6070
61- def dis_between_closest_in_strip (points , points_counts , min_dis = float ("inf" )):
71+ def dis_between_closest_in_strip (
72+ points : list [Any ], points_counts : int , min_dis : float | None = None
73+ ) -> float :
6274 """
6375 closest pair of points in strip
6476
@@ -68,18 +80,22 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")):
6880 Returns :
6981 min_dis (float): distance btw closest pair of points in the strip (< min_dis)
7082
71- >>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
72- 85
83+ >>> dis_between_closest_in_strip([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
84+ 5
7385 """
86+ if min_dis is None :
87+ min_dis = float ("inf" )
7488
75- for i in range (points_counts - 1 ):
76- for j in range (i + 1 , min (i + 6 , points_counts )):
89+ for i in range (points_counts - 1 ):
90+ for j in range (i + 1 , min (i + 6 , points_counts )):
7791 current_dis = euclidean_distance_sqr (points [i ], points [j ])
7892 min_dis = min (min_dis , current_dis )
7993 return min_dis
8094
8195
82- def closest_pair_of_points_sqr (points_sorted_on_x , points_sorted_on_y , points_counts ):
96+ def closest_pair_of_points_sqr (
97+ points_sorted_on_x : list [Any ], points_sorted_on_y : list [Any ], points_counts : int
98+ ) -> float :
8399 """divide and conquer approach
84100
85101 Parameters :
@@ -88,7 +104,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
88104 Returns :
89105 (float): distance btw closest pair of points
90106
91- >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6 ), (7, 8 )], 2)
107+ >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(1, 2 ), (3, 4 )], 2)
92108 8
93109 """
94110
@@ -122,20 +138,22 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
122138 return min (closest_pair_dis , closest_in_strip )
123139
124140
125- def closest_pair_of_points (points , points_counts ) :
141+ def closest_pair_of_points (points : list [ Any ] , points_counts : int ) -> float :
126142 """
127- >>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)]) )
143+ >>> closest_pair_of_points([(2, 3), (12, 30)], 2 )
128144 28.792360097775937
129145 """
130146 points_sorted_on_x = column_based_sort (points , column = 0 )
131147 points_sorted_on_y = column_based_sort (points , column = 1 )
132148 return (
133- closest_pair_of_points_sqr (
134- points_sorted_on_x , points_sorted_on_y , points_counts
135- )
149+ closest_pair_of_points_sqr (points_sorted_on_x , points_sorted_on_y , points_counts )
136150 ) ** 0.5
137151
138152
139153if __name__ == "__main__" :
154+ import doctest
155+
156+ doctest .testmod ()
157+
140158 points = [(2 , 3 ), (12 , 30 ), (40 , 50 ), (5 , 1 ), (12 , 10 ), (3 , 4 )]
141- print ("Distance:" , closest_pair_of_points (points , len (points )))
159+ print (f "Distance: { closest_pair_of_points (points , len (points ))} " )
0 commit comments