mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-15 18:27:36 +00:00
Add static typing to backtracking algorithms (#2684)
* Added static typing to backtracking algorithms * Ran psf/black to fix some minor issues. * updating DIRECTORY.md * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
parent
5de90aafc7
commit
e07766230d
13
DIRECTORY.md
13
DIRECTORY.md
@ -41,6 +41,7 @@
|
|||||||
* [Quine Mc Cluskey](https://github.com/TheAlgorithms/Python/blob/master/boolean_algebra/quine_mc_cluskey.py)
|
* [Quine Mc Cluskey](https://github.com/TheAlgorithms/Python/blob/master/boolean_algebra/quine_mc_cluskey.py)
|
||||||
|
|
||||||
## Cellular Automata
|
## Cellular Automata
|
||||||
|
* [Conways Game Of Life](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/conways_game_of_life.py)
|
||||||
* [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py)
|
* [One Dimensional](https://github.com/TheAlgorithms/Python/blob/master/cellular_automata/one_dimensional.py)
|
||||||
|
|
||||||
## Ciphers
|
## Ciphers
|
||||||
@ -107,6 +108,7 @@
|
|||||||
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
|
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
|
||||||
* [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py)
|
* [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py)
|
||||||
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
|
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)
|
||||||
|
* [Weight Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/weight_conversion.py)
|
||||||
|
|
||||||
## Data Structures
|
## Data Structures
|
||||||
* Binary Tree
|
* Binary Tree
|
||||||
@ -321,10 +323,6 @@
|
|||||||
* [Test Min Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_kruskal.py)
|
* [Test Min Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_kruskal.py)
|
||||||
* [Test Min Spanning Tree Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_prim.py)
|
* [Test Min Spanning Tree Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_prim.py)
|
||||||
|
|
||||||
## Greedy Method
|
|
||||||
* [Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/greedy_method/greedy_knapsack.py)
|
|
||||||
* [Test Knapsack](https://github.com/TheAlgorithms/Python/blob/master/greedy_method/test_knapsack.py)
|
|
||||||
|
|
||||||
## Hashes
|
## Hashes
|
||||||
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
|
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
|
||||||
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
|
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
|
||||||
@ -336,8 +334,11 @@
|
|||||||
* [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py)
|
* [Sha1](https://github.com/TheAlgorithms/Python/blob/master/hashes/sha1.py)
|
||||||
|
|
||||||
## Knapsack
|
## Knapsack
|
||||||
|
* [Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/greedy_knapsack.py)
|
||||||
* [Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/knapsack.py)
|
* [Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/knapsack.py)
|
||||||
* [Test Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/test_knapsack.py)
|
* Tests
|
||||||
|
* [Test Greedy Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/tests/test_greedy_knapsack.py)
|
||||||
|
* [Test Knapsack](https://github.com/TheAlgorithms/Python/blob/master/knapsack/tests/test_knapsack.py)
|
||||||
|
|
||||||
## Linear Algebra
|
## Linear Algebra
|
||||||
* Src
|
* Src
|
||||||
@ -400,6 +401,7 @@
|
|||||||
* [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py)
|
* [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py)
|
||||||
* [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py)
|
* [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py)
|
||||||
* [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py)
|
* [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py)
|
||||||
|
* [Euclidean Distance](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_distance.py)
|
||||||
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
|
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
|
||||||
* [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py)
|
* [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py)
|
||||||
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
|
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
|
||||||
@ -886,6 +888,7 @@
|
|||||||
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
|
* [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py)
|
||||||
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
|
* [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py)
|
||||||
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)
|
* [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py)
|
||||||
|
* [Instagram Video](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_video.py)
|
||||||
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
|
* [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py)
|
||||||
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
|
* [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py)
|
||||||
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)
|
* [Test Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/test_fetch_github_info.py)
|
||||||
|
@ -16,7 +16,13 @@ def generate_all_combinations(n: int, k: int) -> [[int]]:
|
|||||||
return result
|
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:
|
if level == 0:
|
||||||
total_list.append(current_list[:])
|
total_list.append(current_list[:])
|
||||||
return
|
return
|
||||||
@ -27,7 +33,7 @@ def create_all_state(increment, total_number, level, current_list, total_list):
|
|||||||
current_list.pop()
|
current_list.pop()
|
||||||
|
|
||||||
|
|
||||||
def print_all_state(total_list):
|
def print_all_state(total_list: [int]) -> None:
|
||||||
for i in total_list:
|
for i in total_list:
|
||||||
print(*i)
|
print(*i)
|
||||||
|
|
||||||
|
@ -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))])
|
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.
|
Creates a state space tree to iterate through each branch using DFS.
|
||||||
We know that each state has exactly len(sequence) - index children.
|
We know that each state has exactly len(sequence) - index children.
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
solution = []
|
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
|
This function returns a boolean value True if it is safe to place a queen there
|
||||||
considering the current state of the board.
|
considering the current state of the board.
|
||||||
@ -38,7 +38,7 @@ def isSafe(board, row, column):
|
|||||||
return True
|
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
|
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
|
False Boolean and terminates that branch and backtracks to the next
|
||||||
@ -68,7 +68,7 @@ def solve(board, row):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def printboard(board):
|
def printboard(board: [[int]]) -> None:
|
||||||
"""
|
"""
|
||||||
Prints the boards that have a successful combination.
|
Prints the boards that have a successful combination.
|
||||||
"""
|
"""
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
def solve_maze(maze: list) -> bool:
|
def solve_maze(maze: [[int]]) -> bool:
|
||||||
"""
|
"""
|
||||||
This method solves the "rat in maze" problem.
|
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.
|
In this problem we have some n by n matrix, a start point and an end point.
|
||||||
@ -67,7 +67,7 @@ def solve_maze(maze: list) -> bool:
|
|||||||
return solved
|
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:
|
This method is recursive starting from (i, j) and going in one of four directions:
|
||||||
up, down, left, right.
|
up, down, left, right.
|
||||||
|
@ -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 = []
|
result = []
|
||||||
path = []
|
path = []
|
||||||
num_index = 0
|
num_index = 0
|
||||||
@ -17,7 +17,14 @@ def generate_sum_of_subsets_soln(nums, max_sum):
|
|||||||
return result
|
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.
|
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
|
It terminates the branching of a node when any of the two conditions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user