From faea66e6c317dcd770cbe590106d14112ece3186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Sun, 10 Oct 2021 19:57:58 -0500 Subject: [PATCH 01/12] [mypy] fix type annotations for graphs/a_star.py #4052 --- graphs/a_star.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index d3657cb19540..36611c8ba126 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -31,7 +31,13 @@ # function to search the path -def search(grid, init, goal, cost, heuristic): +def search( + grid: "list[list[int]]", + init: "list[int]", + goal: "list[int]", + cost: int, + heuristic: "list[list[int]]", +) -> "list[list[int]]": closed = [ [0 for col in range(len(grid[0]))] for row in range(len(grid)) From d0b1a3d9f26b69b0ceaad0cc2fba373215fc1606 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 11 Oct 2021 00:59:04 +0000 Subject: [PATCH 02/12] updating DIRECTORY.md --- DIRECTORY.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2e9942b5bf93..6c227fd2b5ad 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -274,6 +274,7 @@ * [Test Send File](https://github.com/TheAlgorithms/Python/blob/master/file_transfer/tests/test_send_file.py) ## Fractals + * [Julia Sets](https://github.com/TheAlgorithms/Python/blob/master/fractals/julia_sets.py) * [Koch Snowflake](https://github.com/TheAlgorithms/Python/blob/master/fractals/koch_snowflake.py) * [Mandelbrot](https://github.com/TheAlgorithms/Python/blob/master/fractals/mandelbrot.py) * [Sierpinski Triangle](https://github.com/TheAlgorithms/Python/blob/master/fractals/sierpinski_triangle.py) @@ -424,10 +425,12 @@ * [Binomial Distribution](https://github.com/TheAlgorithms/Python/blob/master/maths/binomial_distribution.py) * [Bisection](https://github.com/TheAlgorithms/Python/blob/master/maths/bisection.py) * [Ceil](https://github.com/TheAlgorithms/Python/blob/master/maths/ceil.py) + * [Check Polygon](https://github.com/TheAlgorithms/Python/blob/master/maths/check_polygon.py) * [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py) * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) * [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py) * [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py) + * [Double Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/double_factorial_iterative.py) * [Double Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/double_factorial_recursive.py) * [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py) * [Euclidean Distance](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_distance.py) @@ -511,6 +514,7 @@ * [Sum Of Arithmetic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_arithmetic_series.py) * [Sum Of Digits](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_digits.py) * [Sum Of Geometric Progression](https://github.com/TheAlgorithms/Python/blob/master/maths/sum_of_geometric_progression.py) + * [Sylvester Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/sylvester_sequence.py) * [Test Prime Check](https://github.com/TheAlgorithms/Python/blob/master/maths/test_prime_check.py) * [Trapezoidal Rule](https://github.com/TheAlgorithms/Python/blob/master/maths/trapezoidal_rule.py) * [Triplet Sum](https://github.com/TheAlgorithms/Python/blob/master/maths/triplet_sum.py) @@ -928,7 +932,6 @@ * [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py) * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) - * [Swap Case](https://github.com/TheAlgorithms/Python/blob/master/strings/swap_case.py) * [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py) * [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py) * [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/strings/word_patterns.py) From 60ec27ac544d4cfc69586f213b28010a9dd806ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 11 Oct 2021 08:07:12 -0500 Subject: [PATCH 03/12] Add from __future__ import anotations --- graphs/a_star.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 36611c8ba126..4cdf31e6d8d5 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,3 +1,5 @@ +from __future__ import annotations + grid = [ [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles @@ -14,7 +16,8 @@ [5, 4, 3, 2, 1, 0]]""" init = [0, 0] -goal = [len(grid) - 1, len(grid[0]) - 1] # all coordinates are given in format [y,x] +# all coordinates are given in format [y,x] +goal = [len(grid) - 1, len(grid[0]) - 1] cost = 1 # the cost map which pushes the path closer to the goal @@ -27,17 +30,18 @@ # the actions we can take -delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # go up # go left # go down # go right +# go up # go left # go down # go right +delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] # function to search the path def search( - grid: "list[list[int]]", - init: "list[int]", - goal: "list[int]", + grid: list[list[int]], + init: list[int], + goal: list[int], cost: int, - heuristic: "list[list[int]]", -) -> "list[list[int]]": + heuristic: list[list[int]], +) -> list[list[int]]: closed = [ [0 for col in range(len(grid[0]))] for row in range(len(grid)) From 070321036e4c564350011d4373666cb394a1e0d0 Mon Sep 17 00:00:00 2001 From: poloso Date: Mon, 18 Oct 2021 11:25:51 -0500 Subject: [PATCH 04/12] rename delta by DIRECTIONS Co-authored-by: John Law --- graphs/a_star.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 4cdf31e6d8d5..19c5c95fe595 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -31,7 +31,7 @@ # the actions we can take # go up # go left # go down # go right -delta = [[-1, 0], [0, -1], [1, 0], [0, 1]] +DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]] # function to search the path From 8d23c2ff067a38529095290afbb136b1c28e5eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:30:40 -0500 Subject: [PATCH 05/12] Rename delta by DIRECTIONS in all code --- graphs/a_star.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 19c5c95fe595..241cda4355ea 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -74,9 +74,9 @@ def search( if x == goal[0] and y == goal[1]: found = True else: - for i in range(len(delta)): # to try out different valid actions - x2 = x + delta[i][0] - y2 = y + delta[i][1] + for i in range(len(DIRECTIONS)): # to try out different valid actions + x2 = x + DIRECTIONS[i][0] + y2 = y + DIRECTIONS[i][1] if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]): if closed[x2][y2] == 0 and grid[x2][y2] == 0: g2 = g + cost @@ -89,8 +89,8 @@ def search( y = goal[1] invpath.append([x, y]) # we get the reverse path from here while x != init[0] or y != init[1]: - x2 = x - delta[action[x][y]][0] - y2 = y - delta[action[x][y]][1] + x2 = x - DIRECTIONS[action[x][y]][0] + y2 = y - DIRECTIONS[action[x][y]][1] x = x2 y = y2 invpath.append([x, y]) From 6714e2ea549dac8b4e66dd438d0561bcaac9c61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:32:12 -0500 Subject: [PATCH 06/12] Enclose script in __main__ code block --- graphs/a_star.py | 65 ++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 241cda4355ea..ced91f061c05 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,34 +1,5 @@ from __future__ import annotations -grid = [ - [0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles - [0, 1, 0, 0, 0, 0], - [0, 1, 0, 0, 1, 0], - [0, 0, 0, 0, 1, 0], -] - -""" -heuristic = [[9, 8, 7, 6, 5, 4], - [8, 7, 6, 5, 4, 3], - [7, 6, 5, 4, 3, 2], - [6, 5, 4, 3, 2, 1], - [5, 4, 3, 2, 1, 0]]""" - -init = [0, 0] -# all coordinates are given in format [y,x] -goal = [len(grid) - 1, len(grid[0]) - 1] -cost = 1 - -# the cost map which pushes the path closer to the goal -heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] -for i in range(len(grid)): - for j in range(len(grid[0])): - heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) - if grid[i][j] == 1: - heuristic[i][j] = 99 # added extra penalty in the heuristic map - - # the actions we can take # go up # go left # go down # go right DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]] @@ -105,6 +76,36 @@ def search( return path -a = search(grid, init, goal, cost, heuristic) -for i in range(len(a)): - print(a[i]) +if __name__ == "__main__": + grid = [ + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], # 0 are free path whereas 1's are obstacles + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 1, 0], + [0, 0, 0, 0, 1, 0], + ] + + """ + heuristic = [[9, 8, 7, 6, 5, 4], + [8, 7, 6, 5, 4, 3], + [7, 6, 5, 4, 3, 2], + [6, 5, 4, 3, 2, 1], + [5, 4, 3, 2, 1, 0]]""" + + init = [0, 0] + # all coordinates are given in format [y,x] + goal = [len(grid) - 1, len(grid[0]) - 1] + cost = 1 + + # the cost map which pushes the path closer to the goal + heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))] + for i in range(len(grid)): + for j in range(len(grid[0])): + heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1]) + if grid[i][j] == 1: + # added extra penalty in the heuristic map + heuristic[i][j] = 99 + + a = search(grid, init, goal, cost, heuristic) + for i in range(len(a)): + print(a[i]) From af7a63604eb6c19316fd22f3662141a89fba8969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 14:44:07 -0500 Subject: [PATCH 07/12] Refactor DIRECTIONS with comments for readibility --- graphs/a_star.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index ced91f061c05..dc4199cdba59 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,8 +1,6 @@ from __future__ import annotations -# the actions we can take -# go up # go left # go down # go right -DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]] +DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]] # left # down # right # up # function to search the path From 29d08f4a21a48f0b5c822432705b7632288d5ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 14:47:58 -0500 Subject: [PATCH 08/12] Delete heuristic example comment --- graphs/a_star.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index dc4199cdba59..83b3f3d03f65 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -83,13 +83,6 @@ def search( [0, 0, 0, 0, 1, 0], ] - """ - heuristic = [[9, 8, 7, 6, 5, 4], - [8, 7, 6, 5, 4, 3], - [7, 6, 5, 4, 3, 2], - [6, 5, 4, 3, 2, 1], - [5, 4, 3, 2, 1, 0]]""" - init = [0, 0] # all coordinates are given in format [y,x] goal = [len(grid) - 1, len(grid[0]) - 1] From ac0896566968019079f765abf7b97c628cc01b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 15:16:37 -0500 Subject: [PATCH 09/12] Do not print, return all values --- graphs/a_star.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 83b3f3d03f65..d65584a55e51 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -10,7 +10,7 @@ def search( goal: list[int], cost: int, heuristic: list[list[int]], -) -> list[list[int]]: +) -> tuple[list[list[int]], list[list[int]]]: closed = [ [0 for col in range(len(grid[0]))] for row in range(len(grid)) @@ -31,7 +31,7 @@ def search( while not found and not resign: if len(cell) == 0: - return "FAIL" + raise ValueError("Algorithm is unable to find solution") else: # to choose the least costliest action so as to move closer to the goal cell.sort() cell.reverse() @@ -67,11 +67,7 @@ def search( path = [] for i in range(len(invpath)): path.append(invpath[len(invpath) - 1 - i]) - print("ACTION MAP") - for i in range(len(action)): - print(action[i]) - - return path + return path, action if __name__ == "__main__": @@ -97,6 +93,11 @@ def search( # added extra penalty in the heuristic map heuristic[i][j] = 99 - a = search(grid, init, goal, cost, heuristic) - for i in range(len(a)): - print(a[i]) + path, action = search(grid, init, goal, cost, heuristic) + + print("ACTION MAP") + for i in range(len(action)): + print(action[i]) + + for i in range(len(path)): + print(path[i]) From c5196de5922279c2538f557dc84a69c4cdbefd4a Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 22 Oct 2021 17:57:16 +0800 Subject: [PATCH 10/12] Fix multilines --- graphs/a_star.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index d65584a55e51..5b0386939c31 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,7 +1,11 @@ from __future__ import annotations -DIRECTIONS = [[-1, 0], [0, -1], [1, 0], [0, 1]] # left # down # right # up - +DIRECTIONS = [ + [-1, 0], # left + [0, -1], # down + [1, 0], # right + [0, 1], # up +] # function to search the path def search( From e607e8c833ae73f306e8c4a6869762c06cf26a92 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 22 Oct 2021 17:59:03 +0800 Subject: [PATCH 11/12] fix black --- graphs/a_star.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/a_star.py b/graphs/a_star.py index 5b0386939c31..c9676da52418 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -1,8 +1,8 @@ from __future__ import annotations DIRECTIONS = [ - [-1, 0], # left - [0, -1], # down + [-1, 0], # left + [0, -1], # down [1, 0], # right [0, 1], # up ] From 6f22436c33a89e547c503cbf0691430b29ca22c3 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 22 Oct 2021 18:02:19 +0800 Subject: [PATCH 12/12] Update a_star.py --- graphs/a_star.py | 1 + 1 file changed, 1 insertion(+) diff --git a/graphs/a_star.py b/graphs/a_star.py index c9676da52418..e0f24734a4cb 100644 --- a/graphs/a_star.py +++ b/graphs/a_star.py @@ -7,6 +7,7 @@ [0, 1], # up ] + # function to search the path def search( grid: list[list[int]],