Skip to content

Commit adfa45e

Browse files
Hasenncclauss
authored andcommitted
Docstrings and formatting improvements (TheAlgorithms#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 <[email protected]>
1 parent b83ae99 commit adfa45e

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

Diff for: backtracking/hamiltonian_cycle.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
5151
"""
5252
Pseudo-Code
5353
Base Case:
54-
1. Chceck if we visited all of vertices
54+
1. Check if we visited all of vertices
5555
1.1 If last visited vertex has path to starting vertex return True either
5656
return False
5757
Recursive Step:
5858
2. Iterate over each vertex
5959
Check if next vertex is valid for transiting from current vertex
6060
2.1 Remember next vertex as next transition
6161
2.2 Do recursive call and check if going to this vertex solves problem
62-
2.3 if next vertex leads to solution return True
63-
2.4 else backtrack, delete remembered vertex
62+
2.3 If next vertex leads to solution return True
63+
2.4 Else backtrack, delete remembered vertex
6464
6565
Case 1: Use exact graph as in main function, with initialized values
6666
>>> graph = [[0, 1, 0, 1, 0],

Diff for: backtracking/rat_in_maze.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
def solve_maze(maze: list) -> bool:
22
"""
3-
This method solves rat in maze algorithm.
4-
In this problem we have n by n matrix and we have start point and end point
5-
we want to go from source to distination. In this matrix 0 are block paths
6-
1 are open paths we can use.
3+
This method solves the "rat in maze" problem.
4+
In this problem we have some n by n matrix, a start point and an end point.
5+
We want to go from the start to the end. In this matrix zeroes represent walls
6+
and ones paths we can use.
77
Parameters :
88
maze(2D matrix) : maze
99
Returns:
10-
Return: True is maze has a solution or False if it does not.
10+
Return: True if the maze has a solution or False if it does not.
1111
>>> maze = [[0, 1, 0, 1, 1],
1212
... [0, 0, 0, 0, 0],
1313
... [1, 0, 1, 0, 1],
@@ -47,13 +47,13 @@ def solve_maze(maze: list) -> bool:
4747
... [0, 1, 0],
4848
... [1, 0, 0]]
4949
>>> solve_maze(maze)
50-
Solution does not exists!
50+
No solution exists!
5151
False
5252
5353
>>> maze = [[0, 1],
5454
... [1, 0]]
5555
>>> solve_maze(maze)
56-
Solution does not exists!
56+
No solution exists!
5757
False
5858
"""
5959
size = len(maze)
@@ -63,16 +63,15 @@ def solve_maze(maze: list) -> bool:
6363
if solved:
6464
print("\n".join(str(row) for row in solutions))
6565
else:
66-
print("Solution does not exists!")
66+
print("No solution exists!")
6767
return solved
6868

6969

7070
def run_maze(maze, i, j, solutions):
7171
"""
72-
This method is recursive method which starts from i and j
73-
and goes with 4 direction option up, down, left, right
74-
if path found to destination it breaks and return True
75-
otherwise False
72+
This method is recursive starting from (i, j) and going in one of four directions:
73+
up, down, left, right.
74+
If a path is found to destination it returns True otherwise it returns False.
7675
Parameters:
7776
maze(2D matrix) : maze
7877
i, j : coordinates of matrix

Diff for: boolean_algebra/quine_mc_cluskey.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def main():
146146
minterms = [
147147
int(x)
148148
for x in input(
149-
"Enter the decimal representation of Minterms 'Spaces Seprated'\n"
149+
"Enter the decimal representation of Minterms 'Spaces Separated'\n"
150150
).split()
151151
]
152152
binary = decimal_to_binary(no_of_variable, minterms)

Diff for: cellular_automata/one_dimensional.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[i
3232
next_generation = []
3333
for i in range(population):
3434
# Get the neighbors of each cell
35-
left_neighbor = 0 if i == 0 else cells[time][i - 1] # special: leftmost cell
36-
right_neighbor = 0 if i == population - 1 else cells[time][i + 1] # rightmost
35+
# Handle neighbours outside bounds by using 0 as their value
36+
left_neighbor = 0 if i == 0 else cells[time][i - 1]
37+
right_neighbor = 0 if i == population - 1 else cells[time][i + 1]
3738
# Define a new cell and add it to the new generation
3839
situation = 7 - int(f"{left_neighbor}{cells[time][i]}{right_neighbor}", 2)
3940
next_generation.append(rule[situation])

0 commit comments

Comments
 (0)