From 85d33bea6c1c5e7d2fc7b27d5afa09857308b35b Mon Sep 17 00:00:00 2001 From: miltonbhowmick Date: Tue, 8 Aug 2023 11:51:06 +0600 Subject: [PATCH] Moved mutable class attributes as per concern from repository test merge --- graphs/uniform_search_cost.py | 148 +++++++++++++++++----------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/graphs/uniform_search_cost.py b/graphs/uniform_search_cost.py index 241f03510..f1ba4268e 100644 --- a/graphs/uniform_search_cost.py +++ b/graphs/uniform_search_cost.py @@ -1,79 +1,79 @@ import heapq +# diagonal clockwise +dxy1 = [ + (1, 1), + (1, 0), + (1, -1), + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), + (0, 1), +] +# diagonal anti-clockwise +dxy2 = [ + (-1, -1), + (-1, 0), + (0, -1), + (0, 1), + (1, 1), + (1, 0), + (1, -1), + (-1, 1), +] + +# start point and end point on same row and column right side +dxy3 = [ + (0, -1), + (-1, -1), + (-1, 0), + (0, 1), + (1, 1), + (1, 0), + (1, -1), + (-1, 1), +] +# start point and end point on same row and column left side +dxy4 = [ + (0, 1), + (1, 1), + (1, 0), + (1, -1), + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), +] +# start point and end point on same column and row down side +dxy5 = [ + (1, 0), + (0, 1), + (1, 1), + (1, -1), + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), +] +# start point and end point on same column and row up side +dxy6 = [ + (0, -1), + (0, 1), + (1, 1), + (1, 0), + (1, -1), + (-1, -1), + (-1, 0), + (-1, 1), +] + class UniformCostSearch: def __init__(self, grid: list[list[int]]) -> None: self.m = len(grid[0]) self.n = len(grid) - # diagonal clockwise - dxy1 = [ - (1, 1), - (1, 0), - (1, -1), - (0, -1), - (-1, -1), - (-1, 0), - (-1, 1), - (0, 1), - ] - # diagonal anti-clockwise - dxy2 = [ - (-1, -1), - (-1, 0), - (0, -1), - (0, 1), - (1, 1), - (1, 0), - (1, -1), - (-1, 1), - ] - - # start point and end point on same row and column right side - dxy3 = [ - (0, -1), - (-1, -1), - (-1, 0), - (0, 1), - (1, 1), - (1, 0), - (1, -1), - (-1, 1), - ] - # start point and end point on same row and column left side - dxy4 = [ - (0, 1), - (1, 1), - (1, 0), - (1, -1), - (0, -1), - (-1, -1), - (-1, 0), - (-1, 1), - ] - # start point and end point on same column and row down side - dxy5 = [ - (1, 0), - (0, 1), - (1, 1), - (1, -1), - (0, -1), - (-1, -1), - (-1, 0), - (-1, 1), - ] - # start point and end point on same column and row up side - dxy6 = [ - (0, -1), - (0, 1), - (1, 1), - (1, 0), - (1, -1), - (-1, -1), - (-1, 0), - (-1, 1), - ] - def get_shortest_path( self, start: list[int], @@ -192,17 +192,17 @@ class UniformCostSearch: prev = [[None for _ in range(self.m)] for _ in range(self.n)] dxy = [] if start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] < 0: - dxy = self.dxy5 + dxy = dxy5 elif start_point[1] - end_point[1] == 0 and start_point[0] - end_point[0] > 0: - dxy = self.dxy6 + dxy = dxy6 elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] < 0: - dxy = self.dxy4 + dxy = dxy4 elif start_point[0] - end_point[0] == 0 and start_point[1] - end_point[1] > 0: - dxy = self.dxy3 + dxy = dxy3 elif start_point[0] - end_point[0] > 0: - dxy = self.dxy2 + dxy = dxy2 elif start_point[0] - end_point[0] < 0: - dxy = self.dxy1 + dxy = dxy1 goal_answer = [] for _ in range(0, len(end_point)): goal_answer.append(10**8)