From 273e2a70d61e916fe20e95ee02830a94a9278239 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Tue, 25 Dec 2018 16:38:30 -0500 Subject: [PATCH 1/2] Python Jarvis March cleanup: change filename from camelCase to snake_case --- .../code/python/{jarvisMarch.py => jarvis_march.py} | 0 contents/jarvis_march/jarvis_march.md | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename contents/jarvis_march/code/python/{jarvisMarch.py => jarvis_march.py} (100%) diff --git a/contents/jarvis_march/code/python/jarvisMarch.py b/contents/jarvis_march/code/python/jarvis_march.py similarity index 100% rename from contents/jarvis_march/code/python/jarvisMarch.py rename to contents/jarvis_march/code/python/jarvis_march.py diff --git a/contents/jarvis_march/jarvis_march.md b/contents/jarvis_march/jarvis_march.md index ff230998a..b1161c329 100644 --- a/contents/jarvis_march/jarvis_march.md +++ b/contents/jarvis_march/jarvis_march.md @@ -37,7 +37,7 @@ Since this algorithm, there have been many other algorithms that have advanced t {% sample lang="js" %} [import, lang:"javascript"](code/javascript/jarvis-march.js) {% sample lang="py" %} -[import, lang:"python"](code/python/jarvisMarch.py) +[import, lang:"python"](code/python/jarvis_march.py) {% sample lang="cpp" %} [import, lang:"cpp"](code/c++/jarvis_march.cpp) {% sample lang="lisp" %} From 8f70ea2cdedff965a79792b2132278857649d57b Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Tue, 25 Dec 2018 16:39:31 -0500 Subject: [PATCH 2/2] Python Jarvis March cleanup: make PEP8 conformant --- .../jarvis_march/code/python/jarvis_march.py | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/contents/jarvis_march/code/python/jarvis_march.py b/contents/jarvis_march/code/python/jarvis_march.py index 34bea40e4..143989f8f 100644 --- a/contents/jarvis_march/code/python/jarvis_march.py +++ b/contents/jarvis_march/code/python/jarvis_march.py @@ -1,38 +1,45 @@ # Is the turn counter clockwise? -def CCW(p1, p2, p3): - return (p3[1]-p1[1])*(p2[0]-p1[0]) >= (p2[1]-p1[1])*(p3[0]-p1[0]) +def ccw(p1, p2, p3): + return (p3[1] - p1[1]) * (p2[0] - p1[0]) \ + >= (p2[1] - p1[1]) * (p3[0] - p1[0]) -def jarvisMarch(gift): - n = len(gift) #Number of points in list - pointOnHull = min(gift) #leftmost point in gift - hull = [pointOnHull] #leftmost point guaranteed to be in hull - +def jarvis_march(gift): + n = len(gift) # Number of points in list + point_on_hull = min(gift) # leftmost point in gift + hull = [point_on_hull] # leftmost point guaranteed to be in hull + while True: - endpoint = gift[0] #Candidate for next point in hull - for j in range(1,n): - if endpoint==pointOnHull or not CCW(gift[j],hull[-1],endpoint): + # Candidate for next point in hull + endpoint = gift[0] + for j in range(1, n): + if endpoint == point_on_hull \ + or not ccw(gift[j], hull[-1], endpoint): endpoint = gift[j] - - pointOnHull = endpoint - - #Check if we have completely wrapped gift - if hull[0]==endpoint: + + point_on_hull = endpoint + + # Check if we have completely wrapped gift + if hull[0] == endpoint: break else: - hull.append(pointOnHull) - + hull.append(point_on_hull) + return hull def main(): - testGift = [(-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9), - (-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9), - (7, -7), (-2, -9), (6, -5), (0, 14), (2, 8)] - hull = jarvisMarch(testGift) - + test_gift = [ + (-5, 2), (5, 7), (-6, -12), (-14, -14), (9, 9), + (-1, -1), (-10, 11), (-6, 15), (-6, -8), (15, -9), + (7, -7), (-2, -9), (6, -5), (0, 14), (2, 8) + ] + hull = jarvis_march(test_gift) + print("The points in the hull are:") for point in hull: print(point) -main() + +if __name__ == "__main__": + main()