Skip to content

Commit 2606f1b

Browse files
authored
[mypy-fix] Type fixes for graham_scan (#5589)
* [mypy] Fixes type annotations in other/graham_scan #4052 + Prefer tuple to list for point x,y pairs * NOP: fixes typo in comment
1 parent f93c7d4 commit 2606f1b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

other/graham_scan.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from sys import maxsize
1515

1616

17-
def graham_scan(points: list[list[int, int]]) -> list[list[int, int]]:
17+
def graham_scan(points: list[tuple[int, int]]) -> list[tuple[int, int]]:
1818
"""Pure implementation of graham scan algorithm in Python
1919
2020
:param points: The unique points on coordinates.
@@ -57,7 +57,7 @@ def graham_scan(points: list[list[int, int]]) -> list[list[int, int]]:
5757
# remove the lowest and the most left point from points for preparing for sort
5858
points.pop(minidx)
5959

60-
def angle_comparer(point: list[int, int], minx: int, miny: int) -> float:
60+
def angle_comparer(point: tuple[int, int], minx: int, miny: int) -> float:
6161
"""Return the angle toward to point from (minx, miny)
6262
6363
:param point: The target point
@@ -66,13 +66,13 @@ def angle_comparer(point: list[int, int], minx: int, miny: int) -> float:
6666
:return: the angle
6767
6868
Examples:
69-
>>> angle_comparer([1,1], 0, 0)
69+
>>> angle_comparer((1,1), 0, 0)
7070
45.0
7171
72-
>>> angle_comparer([100,1], 10, 10)
72+
>>> angle_comparer((100,1), 10, 10)
7373
-5.710593137499642
7474
75-
>>> angle_comparer([5,5], 2, 3)
75+
>>> angle_comparer((5,5), 2, 3)
7676
33.690067525979785
7777
"""
7878
# sort the points accorgind to the angle from the lowest and the most left point
@@ -83,7 +83,7 @@ def angle_comparer(point: list[int, int], minx: int, miny: int) -> float:
8383

8484
sorted_points = sorted(points, key=lambda point: angle_comparer(point, minx, miny))
8585
# This insert actually costs complexity,
86-
# and you should insteadly add (minx, miny) into stack later.
86+
# and you should instead add (minx, miny) into stack later.
8787
# I'm using insert just for easy understanding.
8888
sorted_points.insert(0, (minx, miny))
8989

@@ -95,7 +95,7 @@ class Direction(Enum):
9595
right = 3
9696

9797
def check_direction(
98-
starting: list[int, int], via: list[int, int], target: list[int, int]
98+
starting: tuple[int, int], via: tuple[int, int], target: tuple[int, int]
9999
) -> Direction:
100100
"""Return the direction toward to the line from via to target from starting
101101
@@ -105,13 +105,13 @@ def check_direction(
105105
:return: the Direction
106106
107107
Examples:
108-
>>> check_direction([1,1], [2,2], [3,3])
108+
>>> check_direction((1,1), (2,2), (3,3))
109109
Direction.straight
110110
111-
>>> check_direction([60,1], [-50,199], [30,2])
111+
>>> check_direction((60,1), (-50,199), (30,2))
112112
Direction.left
113113
114-
>>> check_direction([0,0], [5,5], [10,0])
114+
>>> check_direction((0,0), (5,5), (10,0))
115115
Direction.right
116116
"""
117117
x0, y0 = starting
@@ -132,12 +132,12 @@ def check_direction(
132132
# If they are same, it means they are on a same line of convex hull.
133133
if target_angle > via_angle:
134134
return Direction.left
135-
if target_angle == via_angle:
135+
elif target_angle == via_angle:
136136
return Direction.straight
137-
if target_angle < via_angle:
137+
else:
138138
return Direction.right
139139

140-
stack = deque()
140+
stack: deque[tuple[int, int]] = deque()
141141
stack.append(sorted_points[0])
142142
stack.append(sorted_points[1])
143143
stack.append(sorted_points[2])

0 commit comments

Comments
 (0)