Skip to content

Commit c9db30d

Browse files
berquistleios
authored andcommitted
Make Python Jarvis March conform to PEP8, take 2 (#562)
* Python Jarvis March cleanup: change filename from camelCase to snake_case * Python Jarvis March cleanup: make PEP8 conformant
1 parent 21e2f4c commit c9db30d

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

contents/jarvis_march/code/python/jarvisMarch.py

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Is the turn counter clockwise?
2+
def ccw(p1, p2, p3):
3+
return (p3[1] - p1[1]) * (p2[0] - p1[0]) \
4+
>= (p2[1] - p1[1]) * (p3[0] - p1[0])
5+
6+
7+
def jarvis_march(gift):
8+
n = len(gift) # Number of points in list
9+
point_on_hull = min(gift) # leftmost point in gift
10+
hull = [point_on_hull] # leftmost point guaranteed to be in hull
11+
12+
while True:
13+
# Candidate for next point in hull
14+
endpoint = gift[0]
15+
for j in range(1, n):
16+
if endpoint == point_on_hull \
17+
or not ccw(gift[j], hull[-1], endpoint):
18+
endpoint = gift[j]
19+
20+
point_on_hull = endpoint
21+
22+
# Check if we have completely wrapped gift
23+
if hull[0] == endpoint:
24+
break
25+
else:
26+
hull.append(point_on_hull)
27+
28+
return hull
29+
30+
31+
def main():
32+
test_gift = [
33+
(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9),
34+
(-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9),
35+
(7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)
36+
]
37+
hull = jarvis_march(test_gift)
38+
39+
print("The points in the hull are:")
40+
for point in hull:
41+
print(point)
42+
43+
44+
if __name__ == "__main__":
45+
main()

contents/jarvis_march/jarvis_march.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Since this algorithm, there have been many other algorithms that have advanced t
3737
{% sample lang="js" %}
3838
[import, lang:"javascript"](code/javascript/jarvis-march.js)
3939
{% sample lang="py" %}
40-
[import, lang:"python"](code/python/jarvisMarch.py)
40+
[import, lang:"python"](code/python/jarvis_march.py)
4141
{% sample lang="cpp" %}
4242
[import, lang:"cpp"](code/c++/jarvis_march.cpp)
4343
{% sample lang="lisp" %}

0 commit comments

Comments
 (0)