Fix mypy in #2684 (#3987) · zinating/algorithms-python@25164bb · GitHub
Skip to content

Commit 25164bb

Browse files
authored
* Fix mypy in TheAlgorithms#2684 * fix pre-commit
1 parent 0febbd3 commit 25164bb

5 files changed

Lines changed: 34 additions & 22 deletions

File tree

backtracking/all_combinations.py

Lines changed: 6 additions & 5 deletions

backtracking/all_permutations.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
Time complexity: O(n! * n),
66
where n denotes the length of the given sequence.
77
"""
8+
from typing import List, Union
89

910

10-
def generate_all_permutations(sequence: [int]) -> None:
11+
def generate_all_permutations(sequence: List[Union[int, str]]) -> None:
1112
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
1213

1314

1415
def create_state_space_tree(
15-
sequence: [int], current_sequence: [int], index: int, index_used: int
16+
sequence: List[Union[int, str]],
17+
current_sequence: List[Union[int, str]],
18+
index: int,
19+
index_used: List[int],
1620
) -> None:
1721
"""
1822
Creates a state space tree to iterate through each branch using DFS.
@@ -40,8 +44,8 @@ def create_state_space_tree(
4044
sequence = list(map(int, input().split()))
4145
"""
4246

43-
sequence = [3, 1, 2, 4]
47+
sequence: List[Union[int, str]] = [3, 1, 2, 4]
4448
generate_all_permutations(sequence)
4549

46-
sequence = ["A", "B", "C"]
47-
generate_all_permutations(sequence)
50+
sequence_2: List[Union[int, str]] = ["A", "B", "C"]
51+
generate_all_permutations(sequence_2)

backtracking/n_queens.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
diagonal lines.
88
99
"""
10+
from typing import List
11+
1012
solution = []
1113

1214

13-
def isSafe(board: [[int]], row: int, column: int) -> bool:
15+
def isSafe(board: List[List[int]], row: int, column: int) -> bool:
1416
"""
1517
This function returns a boolean value True if it is safe to place a queen there
1618
considering the current state of the board.
@@ -38,7 +40,7 @@ def isSafe(board: [[int]], row: int, column: int) -> bool:
3840
return True
3941

4042

41-
def solve(board: [[int]], row: int) -> bool:
43+
def solve(board: List[List[int]], row: int) -> bool:
4244
"""
4345
It creates a state space tree and calls the safe function until it receives a
4446
False Boolean and terminates that branch and backtracks to the next
@@ -53,7 +55,7 @@ def solve(board: [[int]], row: int) -> bool:
5355
solution.append(board)
5456
printboard(board)
5557
print()
56-
return
58+
return True
5759
for i in range(len(board)):
5860
"""
5961
For every row it iterates through each column to check if it is feasible to
@@ -68,7 +70,7 @@ def solve(board: [[int]], row: int) -> bool:
6870
return False
6971

7072

71-
def printboard(board: [[int]]) -> None:
73+
def printboard(board: List[List[int]]) -> None:
7274
"""
7375
Prints the boards that have a successful combination.
7476
"""

backtracking/rat_in_maze.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
def solve_maze(maze: [[int]]) -> bool:
1+
from typing import List
2+
3+
4+
def solve_maze(maze: List[List[int]]) -> bool:
25
"""
36
This method solves the "rat in maze" problem.
47
In this problem we have some n by n matrix, a start point and an end point.
@@ -67,7 +70,7 @@ def solve_maze(maze: [[int]]) -> bool:
6770
return solved
6871

6972

70-
def run_maze(maze: [[int]], i: int, j: int, solutions: [[int]]) -> bool:
73+
def run_maze(maze: List[List[int]], i: int, j: int, solutions: List[List[int]]) -> bool:
7174
"""
7275
This method is recursive starting from (i, j) and going in one of four directions:
7376
up, down, left, right.
@@ -106,6 +109,7 @@ def run_maze(maze: [[int]], i: int, j: int, solutions: [[int]]) -> bool:
106109

107110
solutions[i][j] = 0
108111
return False
112+
return False
109113

110114

111115
if __name__ == "__main__":

backtracking/sum_of_subsets.py

Lines changed: 7 additions & 6 deletions

0 commit comments

Comments
 (0)