Skip to content

Commit d8b8e88

Browse files
berquistleios
authored andcommitted
Make Graham Scan in Python PEP8 compliant (algorithm-archivists#644)
* Rename Graham Scan in Python according to PEP8 * Apply black to Graham Scan in Python * Turn code comments into docstrings
1 parent 1a4583b commit d8b8e88

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

contents/graham_scan/code/python/grahamScan.py

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from math import atan2
2+
3+
4+
def counter_clockwise(p1, p2, p3):
5+
"""Is the turn counter-clockwise?"""
6+
return (p3[1] - p1[1]) * (p2[0] - p1[0]) >= (p2[1] - p1[1]) * (p3[0] - p1[0])
7+
8+
9+
def polar_angle(ref, point):
10+
"""Find the polar angle of a point relative to a reference point"""
11+
return atan2(point[1] - ref[1], point[0] - ref[0])
12+
13+
14+
def graham_scan(gift):
15+
gift = list(set(gift)) # Remove duplicate points
16+
start = min(gift, key=lambda p: (p[1], p[0])) # Must be in hull
17+
gift.remove(start)
18+
19+
s = sorted(gift, key=lambda point: polar_angle(start, point))
20+
hull = [start, s[0], s[1]]
21+
22+
# Remove points from hull that make the hull concave
23+
for pt in s[2:]:
24+
while not counter_clockwise(hull[-2], hull[-1], pt):
25+
del hull[-1]
26+
hull.append(pt)
27+
28+
return hull
29+
30+
31+
def main():
32+
test_gift = [
33+
(-5, 2),
34+
(5, 7),
35+
(-6, -12),
36+
(-14, -14),
37+
(9, 9),
38+
(-1, -1),
39+
(-10, 11),
40+
(-6, 15),
41+
(-6, -8),
42+
(15, -9),
43+
(7, -7),
44+
(-2, -9),
45+
(6, -5),
46+
(0, 14),
47+
(2, 8),
48+
]
49+
hull = graham_scan(test_gift)
50+
51+
print("The points in the hull are:")
52+
for point in hull:
53+
print(point)
54+
55+
56+
main()

contents/graham_scan/graham_scan.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ We can find whether a rotation is counter-clockwise with trigonometric functions
2121
{% sample lang="js" %}
2222
[import:36-38, lang:"javascript"](code/javascript/graham-scan.js)
2323
{% sample lang="py" %}
24-
[import:4-6, lang:"python"](code/python/grahamScan.py)
24+
[import:4-6, lang:"python"](code/python/graham_scan.py)
2525
{% sample lang="go" %}
2626
[import:13-15, lang:"go"](code/golang/graham.go)
2727
{% sample lang="java" %}
@@ -51,7 +51,7 @@ In the end, the code should look something like this:
5151
{% sample lang="js" %}
5252
[import:1-30, lang:"javascript"](code/javascript/graham-scan.js)
5353
{% sample lang="py" %}
54-
[import:14-27, lang:"python"](code/python/grahamScan.py)
54+
[import:14-28, lang:"python"](code/python/graham_scan.py)
5555
{% sample lang="go" %}
5656
[import:21-42, lang:"go"](code/golang/graham.go)
5757
{% sample lang="java" %}
@@ -76,7 +76,7 @@ In the end, the code should look something like this:
7676
{% sample lang="js" %}
7777
[import, lang:"javascript"](code/javascript/graham-scan.js)
7878
{% sample lang="py" %}
79-
[import, lang:"python"](code/python/grahamScan.py)
79+
[import, lang:"python"](code/python/graham_scan.py)
8080
{% sample lang="go" %}
8181
[import, lang:"go"](code/golang/graham.go)
8282
{% sample lang="java" %}

0 commit comments

Comments
 (0)