Skip to content

Commit 3a0316f

Browse files
author
Simon Lammer
committed
Add type hints to functions
1 parent 7e0fe0e commit 3a0316f

File tree

1 file changed

+19
-35
lines changed

1 file changed

+19
-35
lines changed

Diff for: divide_and_conquer/convex_hull.py

+19-35
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
"""
1515

16+
from typing import Iterable, List, Set, Union
1617

1718
class Point:
1819
"""
@@ -81,7 +82,7 @@ def __hash__(self):
8182
return hash(self.x)
8283

8384

84-
def _construct_points(list_of_tuples):
85+
def _construct_points(list_of_tuples: Union[List[Point], List[List[float]], Iterable[List[float]]]) -> List[Point]:
8586
"""
8687
constructs a list of points from an array-like object of numbers
8788
@@ -113,17 +114,20 @@ def _construct_points(list_of_tuples):
113114
points = []
114115
if list_of_tuples:
115116
for p in list_of_tuples:
116-
try:
117-
points.append(Point(p[0], p[1]))
118-
except (IndexError, TypeError):
119-
print(
120-
f"Ignoring deformed point {p}. All points"
121-
" must have at least 2 coordinates."
122-
)
117+
if isinstance(p, Point):
118+
points.append(p)
119+
else:
120+
try:
121+
points.append(Point(p[0], p[1]))
122+
except (IndexError, TypeError):
123+
print(
124+
f"Ignoring deformed point {p}. All points"
125+
" must have at least 2 coordinates."
126+
)
123127
return points
124128

125129

126-
def _validate_input(points):
130+
def _validate_input(points: Union[List[Point], List[List[float]]]) -> List[Point]:
127131
"""
128132
validates an input instance before a convex-hull algorithms uses it
129133
@@ -168,30 +172,10 @@ def _validate_input(points):
168172
if not points:
169173
raise ValueError(f"Expecting a list of points but got {points}")
170174

171-
if isinstance(points, set):
172-
points = list(points)
173-
174-
try:
175-
if hasattr(points, "__iter__") and not isinstance(points[0], Point):
176-
if isinstance(points[0], (list, tuple)):
177-
points = _construct_points(points)
178-
else:
179-
raise ValueError(
180-
"Expecting an iterable of type Point, list or tuple. "
181-
f"Found objects of type {type(points[0])} instead"
182-
)
183-
elif not hasattr(points, "__iter__"):
184-
raise ValueError(
185-
f"Expecting an iterable object but got an non-iterable type {points}"
186-
)
187-
except TypeError:
188-
print("Expecting an iterable of type Point, list or tuple.")
189-
raise
190-
191-
return points
175+
return _construct_points(points)
192176

193177

194-
def _det(a, b, c):
178+
def _det(a: Point, b: Point, c: Point) -> float:
195179
"""
196180
Computes the sign perpendicular distance of a 2d point c from a line segment
197181
ab. The sign indicates the direction of c relative to ab.
@@ -226,7 +210,7 @@ def _det(a, b, c):
226210
return det
227211

228212

229-
def convex_hull_bf(points):
213+
def convex_hull_bf(points: List[Point]) -> List[Point]:
230214
"""
231215
Constructs the convex hull of a set of 2D points using a brute force algorithm.
232216
The algorithm basically considers all combinations of points (i, j) and uses the
@@ -299,7 +283,7 @@ def convex_hull_bf(points):
299283
return sorted(convex_set)
300284

301285

302-
def convex_hull_recursive(points):
286+
def convex_hull_recursive(points: List[Point]) -> List[Point]:
303287
"""
304288
Constructs the convex hull of a set of 2D points using a divide-and-conquer strategy
305289
The algorithm exploits the geometric properties of the problem by repeatedly
@@ -369,7 +353,7 @@ def convex_hull_recursive(points):
369353
return sorted(convex_set)
370354

371355

372-
def _construct_hull(points, left, right, convex_set):
356+
def _construct_hull(points: List[Point], left: Point, right: Point, convex_set: Set[Point]) -> None:
373357
"""
374358
375359
Parameters
@@ -411,7 +395,7 @@ def _construct_hull(points, left, right, convex_set):
411395
_construct_hull(candidate_points, extreme_point, right, convex_set)
412396

413397

414-
def convex_hull_melkman(points):
398+
def convex_hull_melkman(points: List[Point]) -> List[Point]:
415399
"""
416400
Constructs the convex hull of a set of 2D points using the melkman algorithm.
417401
The algorithm works by iteratively inserting points of a simple polygonal chain

0 commit comments

Comments
 (0)