Skip to content

Commit 25164bb

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

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

backtracking/all_combinations.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
numbers out of 1 ... n. We use backtracking to solve this problem.
44
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
55
"""
6+
from typing import List
67

78

8-
def generate_all_combinations(n: int, k: int) -> [[int]]:
9+
def generate_all_combinations(n: int, k: int) -> List[List[int]]:
910
"""
1011
>>> generate_all_combinations(n=4, k=2)
1112
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
1213
"""
1314

14-
result = []
15+
result: List[List[int]] = []
1516
create_all_state(1, n, k, [], result)
1617
return result
1718

@@ -20,8 +21,8 @@ def create_all_state(
2021
increment: int,
2122
total_number: int,
2223
level: int,
23-
current_list: [int],
24-
total_list: [int],
24+
current_list: List[int],
25+
total_list: List[List[int]],
2526
) -> None:
2627
if level == 0:
2728
total_list.append(current_list[:])
@@ -33,7 +34,7 @@ def create_all_state(
3334
current_list.pop()
3435

3536

36-
def print_all_state(total_list: [int]) -> None:
37+
def print_all_state(total_list: List[List[int]]) -> None:
3738
for i in total_list:
3839
print(*i)
3940

backtracking/all_permutations.py

+9-5
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

+6-4
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

+6-2
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

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,24 @@
66
Summation of the chosen numbers must be equal to given number M and one number
77
can be used only once.
88
"""
9+
from typing import List
910

1011

11-
def generate_sum_of_subsets_soln(nums: [int], max_sum: [int]) -> [int]:
12-
result = []
13-
path = []
12+
def generate_sum_of_subsets_soln(nums: List[int], max_sum: int) -> List[List[int]]:
13+
result: List[List[int]] = []
14+
path: List[int] = []
1415
num_index = 0
1516
remaining_nums_sum = sum(nums)
1617
create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum)
1718
return result
1819

1920

2021
def create_state_space_tree(
21-
nums: [int],
22+
nums: List[int],
2223
max_sum: int,
2324
num_index: int,
24-
path: [int],
25-
result: [int],
25+
path: List[int],
26+
result: List[List[int]],
2627
remaining_nums_sum: int,
2728
) -> None:
2829
"""

0 commit comments

Comments
 (0)