mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Docstrings and formatting improvements (#2418)
* Fix spelling in docstrings * Improve comments and formatting * Update print statement to reflect doctest change * improve phrasing and apply black * Update rat_in_maze.py 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. Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
799fde4c07
commit
10aa214fcb
|
@ -51,7 +51,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
|
||||||
"""
|
"""
|
||||||
Pseudo-Code
|
Pseudo-Code
|
||||||
Base Case:
|
Base Case:
|
||||||
1. Chceck if we visited all of vertices
|
1. Check if we visited all of vertices
|
||||||
1.1 If last visited vertex has path to starting vertex return True either
|
1.1 If last visited vertex has path to starting vertex return True either
|
||||||
return False
|
return False
|
||||||
Recursive Step:
|
Recursive Step:
|
||||||
|
@ -59,8 +59,8 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
|
||||||
Check if next vertex is valid for transiting from current vertex
|
Check if next vertex is valid for transiting from current vertex
|
||||||
2.1 Remember next vertex as next transition
|
2.1 Remember next vertex as next transition
|
||||||
2.2 Do recursive call and check if going to this vertex solves problem
|
2.2 Do recursive call and check if going to this vertex solves problem
|
||||||
2.3 if next vertex leads to solution return True
|
2.3 If next vertex leads to solution return True
|
||||||
2.4 else backtrack, delete remembered vertex
|
2.4 Else backtrack, delete remembered vertex
|
||||||
|
|
||||||
Case 1: Use exact graph as in main function, with initialized values
|
Case 1: Use exact graph as in main function, with initialized values
|
||||||
>>> graph = [[0, 1, 0, 1, 0],
|
>>> graph = [[0, 1, 0, 1, 0],
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
def solve_maze(maze: list) -> bool:
|
def solve_maze(maze: list) -> bool:
|
||||||
"""
|
"""
|
||||||
This method solves rat in maze algorithm.
|
This method solves the "rat in maze" problem.
|
||||||
In this problem we have n by n matrix and we have start point and end point
|
In this problem we have some n by n matrix, a start point and an end point.
|
||||||
we want to go from source to distination. In this matrix 0 are block paths
|
We want to go from the start to the end. In this matrix zeroes represent walls
|
||||||
1 are open paths we can use.
|
and ones paths we can use.
|
||||||
Parameters :
|
Parameters :
|
||||||
maze(2D matrix) : maze
|
maze(2D matrix) : maze
|
||||||
Returns:
|
Returns:
|
||||||
Return: True is maze has a solution or False if it does not.
|
Return: True if the maze has a solution or False if it does not.
|
||||||
>>> maze = [[0, 1, 0, 1, 1],
|
>>> maze = [[0, 1, 0, 1, 1],
|
||||||
... [0, 0, 0, 0, 0],
|
... [0, 0, 0, 0, 0],
|
||||||
... [1, 0, 1, 0, 1],
|
... [1, 0, 1, 0, 1],
|
||||||
|
@ -47,13 +47,13 @@ def solve_maze(maze: list) -> bool:
|
||||||
... [0, 1, 0],
|
... [0, 1, 0],
|
||||||
... [1, 0, 0]]
|
... [1, 0, 0]]
|
||||||
>>> solve_maze(maze)
|
>>> solve_maze(maze)
|
||||||
Solution does not exists!
|
No solution exists!
|
||||||
False
|
False
|
||||||
|
|
||||||
>>> maze = [[0, 1],
|
>>> maze = [[0, 1],
|
||||||
... [1, 0]]
|
... [1, 0]]
|
||||||
>>> solve_maze(maze)
|
>>> solve_maze(maze)
|
||||||
Solution does not exists!
|
No solution exists!
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
size = len(maze)
|
size = len(maze)
|
||||||
|
@ -63,16 +63,15 @@ def solve_maze(maze: list) -> bool:
|
||||||
if solved:
|
if solved:
|
||||||
print("\n".join(str(row) for row in solutions))
|
print("\n".join(str(row) for row in solutions))
|
||||||
else:
|
else:
|
||||||
print("Solution does not exists!")
|
print("No solution exists!")
|
||||||
return solved
|
return solved
|
||||||
|
|
||||||
|
|
||||||
def run_maze(maze, i, j, solutions):
|
def run_maze(maze, i, j, solutions):
|
||||||
"""
|
"""
|
||||||
This method is recursive method which starts from i and j
|
This method is recursive starting from (i, j) and going in one of four directions:
|
||||||
and goes with 4 direction option up, down, left, right
|
up, down, left, right.
|
||||||
if path found to destination it breaks and return True
|
If a path is found to destination it returns True otherwise it returns False.
|
||||||
otherwise False
|
|
||||||
Parameters:
|
Parameters:
|
||||||
maze(2D matrix) : maze
|
maze(2D matrix) : maze
|
||||||
i, j : coordinates of matrix
|
i, j : coordinates of matrix
|
||||||
|
|
|
@ -146,7 +146,7 @@ def main():
|
||||||
minterms = [
|
minterms = [
|
||||||
int(x)
|
int(x)
|
||||||
for x in input(
|
for x in input(
|
||||||
"Enter the decimal representation of Minterms 'Spaces Seprated'\n"
|
"Enter the decimal representation of Minterms 'Spaces Separated'\n"
|
||||||
).split()
|
).split()
|
||||||
]
|
]
|
||||||
binary = decimal_to_binary(no_of_variable, minterms)
|
binary = decimal_to_binary(no_of_variable, minterms)
|
||||||
|
|
|
@ -32,8 +32,9 @@ def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[i
|
||||||
next_generation = []
|
next_generation = []
|
||||||
for i in range(population):
|
for i in range(population):
|
||||||
# Get the neighbors of each cell
|
# Get the neighbors of each cell
|
||||||
left_neighbor = 0 if i == 0 else cells[time][i - 1] # special: leftmost cell
|
# Handle neighbours outside bounds by using 0 as their value
|
||||||
right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # rightmost
|
left_neighbor = 0 if i == 0 else cells[time][i - 1]
|
||||||
|
right_neighbor = 0 if i == population - 1 else cells[time][i + 1]
|
||||||
# Define a new cell and add it to the new generation
|
# Define a new cell and add it to the new generation
|
||||||
situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2)
|
situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2)
|
||||||
next_generation.append(rule[situation])
|
next_generation.append(rule[situation])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user