From 94508213854c0b1874e2cede53c374e69c3408cc Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 00:50:47 +0500 Subject: [PATCH 01/11] UPDATED rat_in_maze.py --- backtracking/rat_in_maze.py | 154 ++++++++++++++++++++++++------------ 1 file changed, 102 insertions(+), 52 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 7bde886dd558..27f5d80120b6 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -1,81 +1,125 @@ from __future__ import annotations -def solve_maze(maze: list[list[int]]) -> bool: +def solve_maze( + maze: list[list[int]], + source_row: int, + source_column: int, + destination_row: int, + destination_column: int, +) -> list[list[int]] | None: """ This method solves the "rat in maze" problem. - In this problem we have some n by n matrix, a start point and an end point. - We want to go from the start to the end. In this matrix zeroes represent walls - and ones paths we can use. Parameters : - maze(2D matrix) : maze + - maze(2D matrix) : maze + - source_row (int): The row index of the starting point. + - source_column (int): The column index of the starting point. + - destination_row (int): The row index of the destination point. + - destination_column (int): The column index of the destination point. Returns: - Return: True if the maze has a solution or False if it does not. + Return: solution(2D matrix) if path exist ,otherwise None. + Description: + This method navigates through a maze represented as an n by n matrix, + starting from a specified source cell and + aiming to reach a destination cell. + The maze consists of walls (1s) and open paths (0s). + By providing custom row and column values, the source and destination + cells can be adjusted. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [1, 0, 1, 0, 1], ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze) - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 0] - [0, 0, 0, 1, 0] - [0, 0, 0, 1, 1] - [0, 0, 0, 0, 1] - True + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1],\ + [1, 1, 1, 0, 0], [1, 1, 1, 1, 0]] + + Note: + In the output maze, the zeros (0s) represent one of the possible + paths from the source to the destination. >>> maze = [[0, 1, 0, 1, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze) - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 0, 0, 0, 0] - [1, 1, 1, 1, 1] - True + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1],\ + [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze) - [1, 1, 1] - [0, 0, 1] - [0, 0, 1] - True + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + [[0, 0, 0], [1, 1, 0], [1, 1, 0]] - >>> maze = [[0, 1, 0], + >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze) - No solution exists! - False + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + [[1, 0, 0], [1, 1, 0], [1, 1, 0]] + + >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], + ... [1, 0, 1, 0, 0, 1, 1, 1], + ... [0, 1, 0, 1, 0, 0, 1, 0], + ... [1, 1, 1, 0, 0, 1, 0, 1], + ... [0, 1, 0, 0, 1, 0, 1, 1], + ... [0, 0, 0, 1, 1, 1, 0, 1], + ... [0, 1, 0, 1, 0, 1, 1, 1], + ... [1, 1, 0, 0, 0, 0, 0, 1]] + >>> solve_maze(maze,0,2,len(maze)-1,2) + [[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1],\ + [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1],\ + [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]] + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 1]] + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + + >>> maze = [[0, 0], + ... [1, 1]] + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) >>> maze = [[0, 1], ... [1, 0]] - >>> solve_maze(maze) - No solution exists! - False + >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + + >>> maze = [[1, 0, 0], + ... [0, 1, 1], + ... [1, 0, 0]] + >>> solve_maze(maze,0,1,len(maze),len(maze)-1) + """ size = len(maze) + # Check if source and destination coordinates are Invalid. + if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or ( + not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1) + ): + return None # We need to create solution object to save path. - solutions = [[0 for _ in range(size)] for _ in range(size)] - solved = run_maze(maze, 0, 0, solutions) + solutions = [[1 for _ in range(size)] for _ in range(size)] + solved = run_maze( + maze, source_row, source_column, destination_row, destination_column, solutions + ) if solved: - print("\n".join(str(row) for row in solutions)) + return solutions else: - print("No solution exists!") - return solved + return None -def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool: +def run_maze( + maze: list[list[int]], + i: int, + j: int, + destination_row: int, + destination_column: int, + solutions: list[list[int]], +) -> list[list[int]] | None: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. If a path is found to destination it returns True otherwise it returns False. - Parameters: + Parameters maze(2D matrix) : maze i, j : coordinates of matrix solutions(2D matrix) : solutions @@ -84,32 +128,38 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) """ size = len(maze) # Final check point. - if i == j == (size - 1): - solutions[i][j] = 1 - return True + if i == destination_row and j == destination_column and maze[i][j] == 0: + solutions[i][j] = 0 + return solutions lower_flag = (not i < 0) and (not j < 0) # Check lower bounds upper_flag = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. - block_flag = (not solutions[i][j]) and (not maze[i][j]) + block_flag = (solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited - solutions[i][j] = 1 + solutions[i][j] = 0 # check for directions if ( - run_maze(maze, i + 1, j, solutions) - or run_maze(maze, i, j + 1, solutions) - or run_maze(maze, i - 1, j, solutions) - or run_maze(maze, i, j - 1, solutions) + run_maze(maze, i + 1, j, destination_row, destination_column, solutions) + or run_maze( + maze, i, j + 1, destination_row, destination_column, solutions + ) + or run_maze( + maze, i - 1, j, destination_row, destination_column, solutions + ) + or run_maze( + maze, i, j - 1, destination_row, destination_column, solutions + ) ): - return True + return solutions - solutions[i][j] = 0 - return False - return False + solutions[i][j] = 1 + return None + return None if __name__ == "__main__": From 9a1d65f845d4c685b5529af92832cca72c0679d1 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 01:09:17 +0500 Subject: [PATCH 02/11] Update reddit.py in Webprogramming b/c it was causing error in pre-commit tests while raising PR. --- web_programming/reddit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/reddit.py b/web_programming/reddit.py index 5ca5f828c0fb..1c165ecc49ec 100644 --- a/web_programming/reddit.py +++ b/web_programming/reddit.py @@ -33,7 +33,7 @@ def get_subreddit_data( headers={"User-agent": "A random string"}, ) if response.status_code == 429: - raise requests.HTTPError + raise requests.HTTPError(response=response) data = response.json() if not wanted_data: From 1b2a9e6f8603b60582b428677290462fa394a9d7 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Sun, 1 Oct 2023 14:43:21 +0500 Subject: [PATCH 03/11] UPDATED rat_in_maze.py --- backtracking/rat_in_maze.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 27f5d80120b6..8168e946b6de 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -7,7 +7,7 @@ def solve_maze( source_column: int, destination_row: int, destination_column: int, -) -> list[list[int]] | None: +) -> list[list[int]]: """ This method solves the "rat in maze" problem. Parameters : @@ -17,7 +17,11 @@ def solve_maze( - destination_row (int): The row index of the destination point. - destination_column (int): The column index of the destination point. Returns: - Return: solution(2D matrix) if path exist ,otherwise None. + - solution (list[list[int]]): A 2D matrix representing the solution path\ + if it exists. + Raises: + - ValueError: If no solution exists or if the source or\ +destination coordinates are invalid. Description: This method navigates through a maze represented as an n by n matrix, starting from a specified source cell and @@ -75,27 +79,38 @@ def solve_maze( ... [0, 1, 1], ... [1, 0, 1]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + Traceback (most recent call last): + ... + ValueError: No solution exists! >>> maze = [[0, 0], ... [1, 1]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + Traceback (most recent call last): + ... + ValueError: No solution exists! >>> maze = [[0, 1], ... [1, 0]] >>> solve_maze(maze,2,0,len(maze)-1,len(maze)-1) + Traceback (most recent call last): + ... + ValueError: Invalid source or destination coordinates >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) - + Traceback (most recent call last): + ... + ValueError: Invalid source or destination coordinates """ size = len(maze) # Check if source and destination coordinates are Invalid. if not (0 <= source_row <= size - 1 and 0 <= source_column <= size - 1) or ( not (0 <= destination_row <= size - 1 and 0 <= destination_column <= size - 1) ): - return None + raise ValueError("Invalid source or destination coordinates") # We need to create solution object to save path. solutions = [[1 for _ in range(size)] for _ in range(size)] solved = run_maze( @@ -104,7 +119,7 @@ def solve_maze( if solved: return solutions else: - return None + raise ValueError("No solution exists!") def run_maze( @@ -114,7 +129,7 @@ def run_maze( destination_row: int, destination_column: int, solutions: list[list[int]], -) -> list[list[int]] | None: +) -> bool: """ This method is recursive starting from (i, j) and going in one of four directions: up, down, left, right. @@ -130,7 +145,7 @@ def run_maze( # Final check point. if i == destination_row and j == destination_column and maze[i][j] == 0: solutions[i][j] = 0 - return solutions + return True lower_flag = (not i < 0) and (not j < 0) # Check lower bounds upper_flag = (i < size) and (j < size) # Check upper bounds @@ -155,11 +170,11 @@ def run_maze( maze, i, j - 1, destination_row, destination_column, solutions ) ): - return solutions + return True solutions[i][j] = 1 - return None - return None + return False + return False if __name__ == "__main__": From 49e6deeb51b4a2681c1ffbb7131924531c43a93b Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 20:53:49 +0500 Subject: [PATCH 04/11] fixed return type to only maze,otherwise raise valueError. --- backtracking/rat_in_maze.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 8168e946b6de..0edf94465145 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -17,8 +17,8 @@ def solve_maze( - destination_row (int): The row index of the destination point. - destination_column (int): The column index of the destination point. Returns: - - solution (list[list[int]]): A 2D matrix representing the solution path\ - if it exists. + - solution (list[list[int]]): A 2D matrix representing the solution path + ... if it exists. Raises: - ValueError: If no solution exists or if the source or\ destination coordinates are invalid. From cedaf3579ac5d05f1204933179216ef09967a43f Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 21:12:08 +0500 Subject: [PATCH 05/11] fixed whitespaces error,improved matrix visual. --- backtracking/rat_in_maze.py | 43 +++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 0edf94465145..b67b231fe68e 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -18,10 +18,10 @@ def solve_maze( - destination_column (int): The column index of the destination point. Returns: - solution (list[list[int]]): A 2D matrix representing the solution path - ... if it exists. + if it exists. Raises: - - ValueError: If no solution exists or if the source or\ -destination coordinates are invalid. + - ValueError: If no solution exists or if the source or + destination coordinates are invalid. Description: This method navigates through a maze represented as an n by n matrix, starting from a specified source cell and @@ -35,8 +35,11 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1],\ - [1, 1, 1, 0, 0], [1, 1, 1, 1, 0]] + [[0, 1, 1, 1, 1], + [0, 0, 0, 0, 1], + [1, 1, 1, 0, 1], + [1, 1, 1, 0, 0], + [1, 1, 1, 1, 0]] Note: In the output maze, the zeros (0s) represent one of the possible @@ -48,20 +51,27 @@ def solve_maze( ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1],\ - [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]] + [[0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], + [0, 0, 0, 0, 0]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 0, 0], [1, 1, 0], [1, 1, 0]] + [[0, 0, 0], + [1, 1, 0], + [1, 1, 0]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[1, 0, 0], [1, 1, 0], [1, 1, 0]] + [[1, 0, 0], + [1, 1, 0], + [1, 1, 0]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], ... [1, 0, 1, 0, 0, 1, 1, 1], @@ -72,9 +82,14 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1],\ - [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1],\ - [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]] + [[1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], + [1, 1, 1, 1, 0, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], + [1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 1, 1, 1, 1, 1], + [1, 1, 0, 1, 1, 1, 1, 1], + [1, 1, 0, 1, 1, 1, 1, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 1], ... [1, 0, 1]] @@ -98,7 +113,7 @@ def solve_maze( ValueError: Invalid source or destination coordinates >>> maze = [[1, 0, 0], - ... [0, 1, 1], + ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze),len(maze)-1) Traceback (most recent call last): @@ -180,4 +195,4 @@ def run_maze( if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE) From 775d6cd9253a6d427bfb49f40d37e7e396b330fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:13:21 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/rat_in_maze.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index b67b231fe68e..a1c8d9a4cd48 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -35,10 +35,10 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], - [0, 0, 0, 0, 1], + [[0, 1, 1, 1, 1], + [0, 0, 0, 0, 1], [1, 1, 1, 0, 1], - [1, 1, 1, 0, 0], + [1, 1, 1, 0, 0], [1, 1, 1, 1, 0]] Note: @@ -51,26 +51,26 @@ def solve_maze( ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], - [0, 1, 1, 1, 1], + [[0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], - [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 0, 0], - [1, 1, 0], + [[0, 0, 0], + [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[1, 0, 0], - [1, 1, 0], + [[1, 0, 0], + [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], @@ -82,13 +82,13 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [[1, 1, 0, 0, 1, 1, 1, 1], - [1, 1, 1, 0, 0, 1, 1, 1], + [[1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1], - [1, 1, 1, 0, 0, 1, 1, 1], - [1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], + [1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1], - [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 1], From c740da3eaa9aee96a44fb2ca0f14a5b5816057b7 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 21:15:50 +0500 Subject: [PATCH 07/11] updated. --- backtracking/rat_in_maze.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index b67b231fe68e..a1c8d9a4cd48 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -35,10 +35,10 @@ def solve_maze( ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], - [0, 0, 0, 0, 1], + [[0, 1, 1, 1, 1], + [0, 0, 0, 0, 1], [1, 1, 1, 0, 1], - [1, 1, 1, 0, 0], + [1, 1, 1, 0, 0], [1, 1, 1, 1, 0]] Note: @@ -51,26 +51,26 @@ def solve_maze( ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 1, 1, 1, 1], - [0, 1, 1, 1, 1], + [[0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], + [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], - [0, 1, 1, 1, 1], [0, 0, 0, 0, 0]] >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) - [[0, 0, 0], - [1, 1, 0], + [[0, 0, 0], + [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) - [[1, 0, 0], - [1, 1, 0], + [[1, 0, 0], + [1, 1, 0], [1, 1, 0]] >>> maze = [[1, 1, 0, 0, 1, 0, 0, 1], @@ -82,13 +82,13 @@ def solve_maze( ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] >>> solve_maze(maze,0,2,len(maze)-1,2) - [[1, 1, 0, 0, 1, 1, 1, 1], - [1, 1, 1, 0, 0, 1, 1, 1], + [[1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1], - [1, 1, 1, 0, 0, 1, 1, 1], - [1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 1, 0, 0, 1, 1, 1], + [1, 1, 0, 0, 1, 1, 1, 1], + [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1], - [1, 1, 0, 1, 1, 1, 1, 1], [1, 1, 0, 1, 1, 1, 1, 1]] >>> maze = [[1, 0, 0], ... [0, 1, 1], From 591c098b6a7f3965f4acd81ebab3a478bd179aa6 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 21:28:57 +0500 Subject: [PATCH 08/11] Try --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index a1c8d9a4cd48..76fd42f6083c 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -18,7 +18,7 @@ def solve_maze( - destination_column (int): The column index of the destination point. Returns: - solution (list[list[int]]): A 2D matrix representing the solution path - if it exists. + if it exists. Raises: - ValueError: If no solution exists or if the source or destination coordinates are invalid. From 1539b6f1791fc99f178d7f9240df0a740cbc5348 Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 22:53:41 +0500 Subject: [PATCH 09/11] updated --- backtracking/rat_in_maze.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 76fd42f6083c..056eb65e6291 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -18,7 +18,7 @@ def solve_maze( - destination_column (int): The column index of the destination point. Returns: - solution (list[list[int]]): A 2D matrix representing the solution path - if it exists. + if it exists. Raises: - ValueError: If no solution exists or if the source or destination coordinates are invalid. @@ -50,7 +50,7 @@ def solve_maze( ... [0, 0, 0, 0, 1], ... [0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE [[0, 1, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 1], @@ -60,7 +60,7 @@ def solve_maze( >>> maze = [[0, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE [[0, 0, 0], [1, 1, 0], [1, 1, 0]] @@ -68,7 +68,7 @@ def solve_maze( >>> maze = [[1, 0, 0], ... [0, 1, 0], ... [1, 0, 0]] - >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) + >>> solve_maze(maze,0,1,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE [[1, 0, 0], [1, 1, 0], [1, 1, 0]] @@ -81,7 +81,7 @@ def solve_maze( ... [0, 0, 0, 1, 1, 1, 0, 1], ... [0, 1, 0, 1, 0, 1, 1, 1], ... [1, 1, 0, 0, 0, 0, 0, 1]] - >>> solve_maze(maze,0,2,len(maze)-1,2) + >>> solve_maze(maze,0,2,len(maze)-1,2) # doctest: +NORMALIZE_WHITESPACE [[1, 1, 0, 0, 1, 1, 1, 1], [1, 1, 1, 0, 0, 1, 1, 1], [1, 1, 1, 1, 0, 1, 1, 1], From e169d07a528b38b1b65322c4b3d8398daa727e2d Mon Sep 17 00:00:00 2001 From: Muhammad Umer Farooq Date: Wed, 4 Oct 2023 23:00:20 +0500 Subject: [PATCH 10/11] updated --- backtracking/rat_in_maze.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 056eb65e6291..8f466a66f62e 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -34,7 +34,7 @@ def solve_maze( ... [1, 0, 1, 0, 1], ... [0, 0, 1, 0, 0], ... [1, 0, 0, 1, 0]] - >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) + >>> solve_maze(maze,0,0,len(maze)-1,len(maze)-1) # doctest: +NORMALIZE_WHITESPACE [[0, 1, 1, 1, 1], [0, 0, 0, 0, 1], [1, 1, 1, 0, 1], From 80138706368c44dae678fe70ac20b2d10b220899 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 4 Oct 2023 20:35:07 +0200 Subject: [PATCH 11/11] Apply suggestions from code review --- backtracking/rat_in_maze.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 8f466a66f62e..626c83cb4a15 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -11,20 +11,19 @@ def solve_maze( """ This method solves the "rat in maze" problem. Parameters : - - maze(2D matrix) : maze - - source_row (int): The row index of the starting point. - - source_column (int): The column index of the starting point. - - destination_row (int): The row index of the destination point. - - destination_column (int): The column index of the destination point. + - maze: A two dimensional matrix of zeros and ones. + - source_row: The row index of the starting point. + - source_column: The column index of the starting point. + - destination_row: The row index of the destination point. + - destination_column: The column index of the destination point. Returns: - - solution (list[list[int]]): A 2D matrix representing the solution path - if it exists. + - solution: A 2D matrix representing the solution path if it exists. Raises: - ValueError: If no solution exists or if the source or destination coordinates are invalid. Description: This method navigates through a maze represented as an n by n matrix, - starting from a specified source cell and + starting from a specified source cell and aiming to reach a destination cell. The maze consists of walls (1s) and open paths (0s). By providing custom row and column values, the source and destination @@ -150,9 +149,9 @@ def run_maze( up, down, left, right. If a path is found to destination it returns True otherwise it returns False. Parameters - maze(2D matrix) : maze + maze: A two dimensional matrix of zeros and ones. i, j : coordinates of matrix - solutions(2D matrix) : solutions + solutions: A two dimensional matrix of solutions. Returns: Boolean if path is found True, Otherwise False. """