14
14
from sys import maxsize
15
15
16
16
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 ]]:
18
18
"""Pure implementation of graham scan algorithm in Python
19
19
20
20
:param points: The unique points on coordinates.
@@ -57,7 +57,7 @@ def graham_scan(points: list[list[int, int]]) -> list[list[int, int]]:
57
57
# remove the lowest and the most left point from points for preparing for sort
58
58
points .pop (minidx )
59
59
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 :
61
61
"""Return the angle toward to point from (minx, miny)
62
62
63
63
:param point: The target point
@@ -66,13 +66,13 @@ def angle_comparer(point: list[int, int], minx: int, miny: int) -> float:
66
66
:return: the angle
67
67
68
68
Examples:
69
- >>> angle_comparer([ 1,1] , 0, 0)
69
+ >>> angle_comparer(( 1,1) , 0, 0)
70
70
45.0
71
71
72
- >>> angle_comparer([ 100,1] , 10, 10)
72
+ >>> angle_comparer(( 100,1) , 10, 10)
73
73
-5.710593137499642
74
74
75
- >>> angle_comparer([ 5,5] , 2, 3)
75
+ >>> angle_comparer(( 5,5) , 2, 3)
76
76
33.690067525979785
77
77
"""
78
78
# 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:
83
83
84
84
sorted_points = sorted (points , key = lambda point : angle_comparer (point , minx , miny ))
85
85
# 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.
87
87
# I'm using insert just for easy understanding.
88
88
sorted_points .insert (0 , (minx , miny ))
89
89
@@ -95,7 +95,7 @@ class Direction(Enum):
95
95
right = 3
96
96
97
97
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 ]
99
99
) -> Direction :
100
100
"""Return the direction toward to the line from via to target from starting
101
101
@@ -105,13 +105,13 @@ def check_direction(
105
105
:return: the Direction
106
106
107
107
Examples:
108
- >>> check_direction([ 1,1], [ 2,2], [ 3,3] )
108
+ >>> check_direction(( 1,1), ( 2,2), ( 3,3) )
109
109
Direction.straight
110
110
111
- >>> check_direction([ 60,1], [ -50,199], [ 30,2] )
111
+ >>> check_direction(( 60,1), ( -50,199), ( 30,2) )
112
112
Direction.left
113
113
114
- >>> check_direction([ 0,0], [ 5,5], [ 10,0] )
114
+ >>> check_direction(( 0,0), ( 5,5), ( 10,0) )
115
115
Direction.right
116
116
"""
117
117
x0 , y0 = starting
@@ -132,12 +132,12 @@ def check_direction(
132
132
# If they are same, it means they are on a same line of convex hull.
133
133
if target_angle > via_angle :
134
134
return Direction .left
135
- if target_angle == via_angle :
135
+ elif target_angle == via_angle :
136
136
return Direction .straight
137
- if target_angle < via_angle :
137
+ else :
138
138
return Direction .right
139
139
140
- stack = deque ()
140
+ stack : deque [ tuple [ int , int ]] = deque ()
141
141
stack .append (sorted_points [0 ])
142
142
stack .append (sorted_points [1 ])
143
143
stack .append (sorted_points [2 ])
0 commit comments