From 26ec9c1154dc526444ca91ff796da78f4a05c061 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 27 Sep 2021 00:39:45 +0800 Subject: [PATCH 01/12] Fix bugs and typo errors in closest pair of points There are typo errors in closest_pair_points function and errors in dis_between_closest_in_strip function. --- divide_and_conquer/closest_pair_of_points.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index cb7fa00d1c8f..a456bab004c4 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -73,8 +73,8 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): 85 """ - for i in range(min(6, points_counts - 1), points_counts): - for j in range(max(0, i - 6), i): + for i in range(points_counts-1): + for j in range(i+1, min(i + 6, points_counts)): current_dis = euclidean_distance_sqr(points[i], points[j]) if current_dis < min_dis: min_dis = current_dis @@ -101,10 +101,10 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co # recursion mid = points_counts // 2 closest_in_left = closest_pair_of_points_sqr( - points_sorted_on_x, points_sorted_on_y[:mid], mid + points_sorted_on_x[:mid], points_sorted_on_y, mid ) closest_in_right = closest_pair_of_points_sqr( - points_sorted_on_y, points_sorted_on_y[mid:], points_counts - mid + points_sorted_on_x[mid:], points_sorted_on_y, points_counts - mid ) closest_pair_dis = min(closest_in_left, closest_in_right) @@ -114,7 +114,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co """ cross_strip = [] - for point in points_sorted_on_x: + for point in points_sorted_on_y: if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis: cross_strip.append(point) From d3059edd1ba2ed913188025cca6df381f98616b9 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 27 Sep 2021 00:41:00 +0800 Subject: [PATCH 02/12] Fix bugs and typo errors in closest pair of points There are typo errors in closest_pair_points function and errors in dis_between_closest_in_strip function. From dd1cbe91dc2c2edd5c1d52b024fcc2aeb25c3867 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 27 Sep 2021 00:42:53 +0800 Subject: [PATCH 03/12] Fix bugs and typo errors in closest pair of points There are typo errors in closest_pair_points function and errors in dis_between_closest_in_strip function. From ac739cfaa8957882d85f2f2b1f3c1432b26f5998 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 27 Sep 2021 00:43:08 +0800 Subject: [PATCH 04/12] Fix bugs and typo errors in closest pair of points There are typo errors in closest_pair_points function and errors in dis_between_closest_in_strip function. From a7b1937c9e635e585feeb81082027aa0a63e01fa Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 02:50:29 +0000 Subject: [PATCH 05/12] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a73c630bc8a7..568fc5e67398 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -469,6 +469,11 @@ ## Geometry * [Geometry](geometry/geometry.py) + * [Graham Scan](geometry/graham_scan.py) + * [Jarvis March](geometry/jarvis_march.py) + * Tests + * [Test Graham Scan](geometry/tests/test_graham_scan.py) + * [Test Jarvis March](geometry/tests/test_jarvis_march.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) From e24be4e6d4b7f24a1dd610028dbdd130c9a70477 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 11:11:48 +0800 Subject: [PATCH 06/12] Fix bugs and typo errors in closest_pair_of_points.py Summary This pull request addresses critical logic errors and typos in the closest_pair_of_points.py algorithm. These fixes ensure the algorithm produces mathematically correct results and passes all automated tests. Key Fixes: Loop Logic Correction: In dis_between_closest_in_strip , the search loop was modified to correctly check all necessary pairs within the strip optimization. Recursion Parameter Fix: Corrected a typo in closest_pair_of_points_sqr where the X-sorted list was not being correctly partitioned for recursive calls. Doctest Alignment: Updated the doctest in dis_between_closest_in_strip from 85 to 5 to reflect the correct mathematical result after the logic fix. The previous expected value of 85 was a result of the bugged implementation. --- divide_and_conquer/closest_pair_of_points.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index d26a4336152b..d7d926cf38a4 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -69,7 +69,7 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): min_dis (float): distance btw closest pair of points in the strip (< min_dis) >>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5) - 85 + 5 """ for i in range(points_counts-1): @@ -88,7 +88,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co Returns : (float): distance btw closest pair of points - >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6), (7, 8)], 2) + >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(1, 2), (3, 4)], 2) 8 """ @@ -124,7 +124,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co def closest_pair_of_points(points, points_counts): """ - >>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)])) + >>> closest_pair_of_points([(2, 3), (12, 30)], 2) 28.792360097775937 """ points_sorted_on_x = column_based_sort(points, column=0) From aeff7546e6dcfb53fc8dd1c7f2ee152b9ca4a192 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 03:27:50 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/closest_pair_of_points.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index d7d926cf38a4..d02ea9b2cc62 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -72,8 +72,8 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): 5 """ - for i in range(points_counts-1): - for j in range(i+1, min(i + 6, points_counts)): + for i in range(points_counts - 1): + for j in range(i + 1, min(i + 6, points_counts)): current_dis = euclidean_distance_sqr(points[i], points[j]) min_dis = min(min_dis, current_dis) return min_dis From 789c6172d95c1fb354d794fafaf547e030bcddc2 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 11:37:08 +0800 Subject: [PATCH 08/12] Fix bugs, add type hints, and revert accidental DIRECTORY.md edit --- DIRECTORY.md | 5 ---- divide_and_conquer/closest_pair_of_points.py | 25 +++++++++++--------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 568fc5e67398..a73c630bc8a7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -469,11 +469,6 @@ ## Geometry * [Geometry](geometry/geometry.py) - * [Graham Scan](geometry/graham_scan.py) - * [Jarvis March](geometry/jarvis_march.py) - * Tests - * [Test Graham Scan](geometry/tests/test_graham_scan.py) - * [Test Jarvis March](geometry/tests/test_jarvis_march.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index d7d926cf38a4..b500b68693dc 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -18,17 +18,20 @@ Time complexity: O(n * log n) """ +from __future__ import annotations +from typing import Any -def euclidean_distance_sqr(point1, point2): + +def euclidean_distance_sqr(point1: list[Any] | tuple[Any, ...], point2: list[Any] | tuple[Any, ...]) -> float: """ - >>> euclidean_distance_sqr([1,2],[2,4]) + >>> euclidean_distance_sqr([1, 2], [2, 4]) 5 """ return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2 -def column_based_sort(array, column=0): +def column_based_sort(array: list[Any], column: int = 0) -> list[Any]: """ >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1) [(3, 0), (5, 1), (4, 2)] @@ -36,7 +39,7 @@ def column_based_sort(array, column=0): return sorted(array, key=lambda x: x[column]) -def dis_between_closest_pair(points, points_counts, min_dis=float("inf")): +def dis_between_closest_pair(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float: """ brute force approach to find distance between closest pair points @@ -46,7 +49,7 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")): Returns : min_dis (float): distance between closest pair of points - >>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5) + >>> dis_between_closest_pair([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5) 5 """ @@ -58,7 +61,7 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")): return min_dis -def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): +def dis_between_closest_in_strip(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float: """ closest pair of points in strip @@ -68,18 +71,18 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")): Returns : min_dis (float): distance btw closest pair of points in the strip (< min_dis) - >>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5) + >>> dis_between_closest_in_strip([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5) 5 """ - for i in range(points_counts-1): - for j in range(i+1, min(i + 6, points_counts)): + for i in range(points_counts - 1): + for j in range(i + 1, min(i + 6, points_counts)): current_dis = euclidean_distance_sqr(points[i], points[j]) min_dis = min(min_dis, current_dis) return min_dis -def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_counts): +def closest_pair_of_points_sqr(points_sorted_on_x: list[Any], points_sorted_on_y: list[Any], points_counts: int) -> float: """divide and conquer approach Parameters : @@ -122,7 +125,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co return min(closest_pair_dis, closest_in_strip) -def closest_pair_of_points(points, points_counts): +def closest_pair_of_points(points: list[Any], points_counts: int) -> float: """ >>> closest_pair_of_points([(2, 3), (12, 30)], 2) 28.792360097775937 From 80787c10e7da6a1e6bc6b442af6e5dc2425c0404 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 03:46:16 +0000 Subject: [PATCH 09/12] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a73c630bc8a7..568fc5e67398 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -469,6 +469,11 @@ ## Geometry * [Geometry](geometry/geometry.py) + * [Graham Scan](geometry/graham_scan.py) + * [Jarvis March](geometry/jarvis_march.py) + * Tests + * [Test Graham Scan](geometry/tests/test_graham_scan.py) + * [Test Jarvis March](geometry/tests/test_jarvis_march.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) From 73c99bcea74577c06d4490eba23f529e7eb2ab91 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 11:49:25 +0800 Subject: [PATCH 10/12] Update DIRECTORY.md --- DIRECTORY.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 568fc5e67398..a73c630bc8a7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -469,11 +469,6 @@ ## Geometry * [Geometry](geometry/geometry.py) - * [Graham Scan](geometry/graham_scan.py) - * [Jarvis March](geometry/jarvis_march.py) - * Tests - * [Test Graham Scan](geometry/tests/test_graham_scan.py) - * [Test Jarvis March](geometry/tests/test_jarvis_march.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) From 02c9f8841036fd1c134205490300279a09f5a8f4 Mon Sep 17 00:00:00 2001 From: Roy027 Date: Mon, 9 Mar 2026 03:49:41 +0000 Subject: [PATCH 11/12] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index a73c630bc8a7..568fc5e67398 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -469,6 +469,11 @@ ## Geometry * [Geometry](geometry/geometry.py) + * [Graham Scan](geometry/graham_scan.py) + * [Jarvis March](geometry/jarvis_march.py) + * Tests + * [Test Graham Scan](geometry/tests/test_graham_scan.py) + * [Test Jarvis March](geometry/tests/test_jarvis_march.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) From ea862574af77ce0fa2f3aee19636f15618354a66 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Mar 2026 04:00:48 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/closest_pair_of_points.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/divide_and_conquer/closest_pair_of_points.py b/divide_and_conquer/closest_pair_of_points.py index b500b68693dc..533432f5a9dd 100644 --- a/divide_and_conquer/closest_pair_of_points.py +++ b/divide_and_conquer/closest_pair_of_points.py @@ -18,12 +18,15 @@ Time complexity: O(n * log n) """ + from __future__ import annotations from typing import Any -def euclidean_distance_sqr(point1: list[Any] | tuple[Any, ...], point2: list[Any] | tuple[Any, ...]) -> float: +def euclidean_distance_sqr( + point1: list[Any] | tuple[Any, ...], point2: list[Any] | tuple[Any, ...] +) -> float: """ >>> euclidean_distance_sqr([1, 2], [2, 4]) 5 @@ -39,7 +42,9 @@ def column_based_sort(array: list[Any], column: int = 0) -> list[Any]: return sorted(array, key=lambda x: x[column]) -def dis_between_closest_pair(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float: +def dis_between_closest_pair( + points: list[Any], points_counts: int, min_dis: float = float("inf") +) -> float: """ brute force approach to find distance between closest pair points @@ -61,7 +66,9 @@ def dis_between_closest_pair(points: list[Any], points_counts: int, min_dis: flo return min_dis -def dis_between_closest_in_strip(points: list[Any], points_counts: int, min_dis: float = float("inf")) -> float: +def dis_between_closest_in_strip( + points: list[Any], points_counts: int, min_dis: float = float("inf") +) -> float: """ closest pair of points in strip @@ -82,7 +89,9 @@ def dis_between_closest_in_strip(points: list[Any], points_counts: int, min_dis: return min_dis -def closest_pair_of_points_sqr(points_sorted_on_x: list[Any], points_sorted_on_y: list[Any], points_counts: int) -> float: +def closest_pair_of_points_sqr( + points_sorted_on_x: list[Any], points_sorted_on_y: list[Any], points_counts: int +) -> float: """divide and conquer approach Parameters :