diff --git a/backtracking/all_combinations.py b/backtracking/all_combinations.py index 0444ed093..76462837c 100644 --- a/backtracking/all_combinations.py +++ b/backtracking/all_combinations.py @@ -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 @@ -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[:]) @@ -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) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 59c7b7bbf..a0032c5ca 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -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. @@ -40,8 +44,8 @@ print("Enter the elements") 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) diff --git a/backtracking/n_queens.py b/backtracking/n_queens.py index 31696b4a8..29b8d819a 100644 --- a/backtracking/n_queens.py +++ b/backtracking/n_queens.py @@ -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. @@ -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 @@ -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 @@ -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. """ diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 8dc484c3f..cd2a8f41d 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -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. @@ -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. @@ -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__": diff --git a/backtracking/sum_of_subsets.py b/backtracking/sum_of_subsets.py index b71edc2ee..f695b8f7a 100644 --- a/backtracking/sum_of_subsets.py +++ b/backtracking/sum_of_subsets.py @@ -6,11 +6,12 @@ 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) @@ -18,11 +19,11 @@ def generate_sum_of_subsets_soln(nums: [int], max_sum: [int]) -> [int]: 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: """