Skip to content

Fix mypy in #2684 #3987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions backtracking/all_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
numbers out of 1 ... n. We use backtracking to solve this problem.
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
"""
from typing import List


def generate_all_combinations(n: int, k: int) -> [[int]]:
def generate_all_combinations(n: int, k: int) -> List[List[int]]:
"""
>>> generate_all_combinations(n=4, k=2)
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
"""

result = []
result: List[List[int]] = []
create_all_state(1, n, k, [], result)
return result

Expand All @@ -20,8 +21,8 @@ def create_all_state(
increment: int,
total_number: int,
level: int,
current_list: [int],
total_list: [int],
current_list: List[int],
total_list: List[List[int]],
) -> None:
if level == 0:
total_list.append(current_list[:])
Expand All @@ -33,7 +34,7 @@ def create_all_state(
current_list.pop()


def print_all_state(total_list: [int]) -> None:
def print_all_state(total_list: List[List[int]]) -> None:
for i in total_list:
print(*i)

Expand Down
14 changes: 9 additions & 5 deletions backtracking/all_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
Time complexity: O(n! * n),
where n denotes the length of the given sequence.
"""
from typing import List, Union


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


def create_state_space_tree(
sequence: [int], current_sequence: [int], index: int, index_used: int
sequence: List[Union[int, str]],
current_sequence: List[Union[int, str]],
index: int,
index_used: List[int],
) -> None:
"""
Creates a state space tree to iterate through each branch using DFS.
Expand Down Expand Up @@ -40,8 +44,8 @@ def create_state_space_tree(
sequence = list(map(int, input().split()))
"""

sequence = [3, 1, 2, 4]
sequence: List[Union[int, str]] = [3, 1, 2, 4]
generate_all_permutations(sequence)

sequence = ["A", "B", "C"]
generate_all_permutations(sequence)
sequence_2: List[Union[int, str]] = ["A", "B", "C"]
generate_all_permutations(sequence_2)
10 changes: 6 additions & 4 deletions backtracking/n_queens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
diagonal lines.

"""
from typing import List

solution = []


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


def solve(board: [[int]], row: int) -> bool:
def solve(board: List[List[int]], row: int) -> bool:
"""
It creates a state space tree and calls the safe function until it receives a
False Boolean and terminates that branch and backtracks to the next
Expand All @@ -53,7 +55,7 @@ def solve(board: [[int]], row: int) -> bool:
solution.append(board)
printboard(board)
print()
return
return True
for i in range(len(board)):
"""
For every row it iterates through each column to check if it is feasible to
Expand All @@ -68,7 +70,7 @@ def solve(board: [[int]], row: int) -> bool:
return False


def printboard(board: [[int]]) -> None:
def printboard(board: List[List[int]]) -> None:
"""
Prints the boards that have a successful combination.
"""
Expand Down
8 changes: 6 additions & 2 deletions backtracking/rat_in_maze.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
def solve_maze(maze: [[int]]) -> bool:
from typing import List


def solve_maze(maze: List[List[int]]) -> bool:
"""
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.
Expand Down Expand Up @@ -67,7 +70,7 @@ def solve_maze(maze: [[int]]) -> bool:
return solved


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

solutions[i][j] = 0
return False
return False


if __name__ == "__main__":
Expand Down
13 changes: 7 additions & 6 deletions backtracking/sum_of_subsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
Summation of the chosen numbers must be equal to given number M and one number
can be used only once.
"""
from typing import List


def generate_sum_of_subsets_soln(nums: [int], max_sum: [int]) -> [int]:
result = []
path = []
def generate_sum_of_subsets_soln(nums: List[int], max_sum: int) -> List[List[int]]:
result: List[List[int]] = []
path: List[int] = []
num_index = 0
remaining_nums_sum = sum(nums)
create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum)
return result


def create_state_space_tree(
nums: [int],
nums: List[int],
max_sum: int,
num_index: int,
path: [int],
result: [int],
path: List[int],
result: List[List[int]],
remaining_nums_sum: int,
) -> None:
"""
Expand Down