Skip to content

Added static typing to backtracking algorithms #2684

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
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_071/sol1.py)
* Problem 072
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol1.py)
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol2.py)
* Problem 074
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol1.py)
* Problem 076
Expand All @@ -689,6 +690,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_125/sol1.py)
* Problem 173
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py)
* Problem 174
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py)
* Problem 191
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py)
* Problem 234
Expand Down
10 changes: 8 additions & 2 deletions backtracking/all_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ def generate_all_combinations(n: int, k: int) -> [[int]]:
return result


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


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

Expand Down
6 changes: 4 additions & 2 deletions backtracking/all_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"""


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


def create_state_space_tree(sequence, current_sequence, index, index_used):
def create_state_space_tree(
sequence: [int], current_sequence: [int], index: int, index_used: int
) -> None:
"""
Creates a state space tree to iterate through each branch using DFS.
We know that each state has exactly len(sequence) - index children.
Expand Down
6 changes: 3 additions & 3 deletions backtracking/n_queens.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
solution = []


def isSafe(board, row, column):
def isSafe(board: [[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 +38,7 @@ def isSafe(board, row, column):
return True


def solve(board, row):
def solve(board: [[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 Down Expand Up @@ -68,7 +68,7 @@ def solve(board, row):
return False


def printboard(board):
def printboard(board: [[int]]) -> None:
"""
Prints the boards that have a successful combination.
"""
Expand Down
4 changes: 2 additions & 2 deletions backtracking/rat_in_maze.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def solve_maze(maze: list) -> bool:
def solve_maze(maze: [[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 +67,7 @@ def solve_maze(maze: list) -> bool:
return solved


def run_maze(maze, i, j, solutions):
def run_maze(maze: [[int]], i: int, j: int, solutions: [[int]]) -> bool:
"""
This method is recursive starting from (i, j) and going in one of four directions:
up, down, left, right.
Expand Down
11 changes: 9 additions & 2 deletions backtracking/sum_of_subsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""


def generate_sum_of_subsets_soln(nums, max_sum):
def generate_sum_of_subsets_soln(nums: [int], max_sum: [int]) -> [int]:
result = []
path = []
num_index = 0
Expand All @@ -17,7 +17,14 @@ def generate_sum_of_subsets_soln(nums, max_sum):
return result


def create_state_space_tree(nums, max_sum, num_index, path, result, remaining_nums_sum):
def create_state_space_tree(
nums: [int],
max_sum: int,
num_index: int,
path: [int],
result: [int],
remaining_nums_sum: int,
) -> None:
"""
Creates a state space tree to iterate through each branch using DFS.
It terminates the branching of a node when any of the two conditions
Expand Down