mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
Pyupgrade to Python 3.9 (#4718)
* Pyupgrade to Python 3.9 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -3,16 +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
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def generate_all_combinations(n: int, k: int) -> List[List[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: List[List[int]] = []
|
||||
result: list[list[int]] = []
|
||||
create_all_state(1, n, k, [], result)
|
||||
return result
|
||||
|
||||
@ -21,8 +21,8 @@ def create_all_state(
|
||||
increment: int,
|
||||
total_number: int,
|
||||
level: int,
|
||||
current_list: List[int],
|
||||
total_list: List[List[int]],
|
||||
current_list: list[int],
|
||||
total_list: list[list[int]],
|
||||
) -> None:
|
||||
if level == 0:
|
||||
total_list.append(current_list[:])
|
||||
@ -34,7 +34,7 @@ def create_all_state(
|
||||
current_list.pop()
|
||||
|
||||
|
||||
def print_all_state(total_list: List[List[int]]) -> None:
|
||||
def print_all_state(total_list: list[list[int]]) -> None:
|
||||
for i in total_list:
|
||||
print(*i)
|
||||
|
||||
|
@ -5,18 +5,18 @@
|
||||
Time complexity: O(n! * n),
|
||||
where n denotes the length of the given sequence.
|
||||
"""
|
||||
from typing import List, Union
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def generate_all_permutations(sequence: List[Union[int, str]]) -> None:
|
||||
def generate_all_permutations(sequence: list[int | str]) -> None:
|
||||
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
|
||||
|
||||
|
||||
def create_state_space_tree(
|
||||
sequence: List[Union[int, str]],
|
||||
current_sequence: List[Union[int, str]],
|
||||
sequence: list[int | str],
|
||||
current_sequence: list[int | str],
|
||||
index: int,
|
||||
index_used: List[int],
|
||||
index_used: list[int],
|
||||
) -> None:
|
||||
"""
|
||||
Creates a state space tree to iterate through each branch using DFS.
|
||||
@ -44,8 +44,8 @@ print("Enter the elements")
|
||||
sequence = list(map(int, input().split()))
|
||||
"""
|
||||
|
||||
sequence: List[Union[int, str]] = [3, 1, 2, 4]
|
||||
sequence: list[int | str] = [3, 1, 2, 4]
|
||||
generate_all_permutations(sequence)
|
||||
|
||||
sequence_2: List[Union[int, str]] = ["A", "B", "C"]
|
||||
sequence_2: list[int | str] = ["A", "B", "C"]
|
||||
generate_all_permutations(sequence_2)
|
||||
|
@ -5,15 +5,17 @@ of the given sequence. We use backtracking to solve this problem.
|
||||
Time complexity: O(2^n),
|
||||
where n denotes the length of the given sequence.
|
||||
"""
|
||||
from typing import Any, List
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def generate_all_subsequences(sequence: List[Any]) -> None:
|
||||
def generate_all_subsequences(sequence: list[Any]) -> None:
|
||||
create_state_space_tree(sequence, [], 0)
|
||||
|
||||
|
||||
def create_state_space_tree(
|
||||
sequence: List[Any], current_subsequence: List[Any], index: int
|
||||
sequence: list[Any], current_subsequence: list[Any], index: int
|
||||
) -> None:
|
||||
"""
|
||||
Creates a state space tree to iterate through each branch using DFS.
|
||||
@ -32,7 +34,7 @@ def create_state_space_tree(
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
seq: List[Any] = [3, 1, 2, 4]
|
||||
seq: list[Any] = [3, 1, 2, 4]
|
||||
generate_all_subsequences(seq)
|
||||
|
||||
seq.clear()
|
||||
|
@ -5,11 +5,10 @@
|
||||
|
||||
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
|
||||
def valid_coloring(
|
||||
neighbours: List[int], colored_vertices: List[int], color: int
|
||||
neighbours: list[int], colored_vertices: list[int], color: int
|
||||
) -> bool:
|
||||
"""
|
||||
For each neighbour check if coloring constraint is satisfied
|
||||
@ -35,7 +34,7 @@ def valid_coloring(
|
||||
|
||||
|
||||
def util_color(
|
||||
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
|
||||
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
|
||||
) -> bool:
|
||||
"""
|
||||
Pseudo-Code
|
||||
@ -86,7 +85,7 @@ def util_color(
|
||||
return False
|
||||
|
||||
|
||||
def color(graph: List[List[int]], max_colors: int) -> List[int]:
|
||||
def color(graph: list[list[int]], max_colors: int) -> list[int]:
|
||||
"""
|
||||
Wrapper function to call subroutine called util_color
|
||||
which will either return True or False.
|
||||
|
@ -6,11 +6,10 @@
|
||||
|
||||
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
|
||||
"""
|
||||
from typing import List
|
||||
|
||||
|
||||
def valid_connection(
|
||||
graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int]
|
||||
graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
|
||||
) -> bool:
|
||||
"""
|
||||
Checks whether it is possible to add next into path by validating 2 statements
|
||||
@ -47,7 +46,7 @@ def valid_connection(
|
||||
return not any(vertex == next_ver for vertex in path)
|
||||
|
||||
|
||||
def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
|
||||
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
|
||||
"""
|
||||
Pseudo-Code
|
||||
Base Case:
|
||||
@ -108,7 +107,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
|
||||
return False
|
||||
|
||||
|
||||
def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
|
||||
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
|
||||
r"""
|
||||
Wrapper function to call subroutine called util_hamilton_cycle,
|
||||
which will either return array of vertices indicating hamiltonian cycle
|
||||
|
@ -1,9 +1,9 @@
|
||||
# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM
|
||||
|
||||
from typing import List, Tuple
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
|
||||
def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
|
||||
"""
|
||||
Find all the valid positions a knight can move to from the current position.
|
||||
|
||||
@ -32,7 +32,7 @@ def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
|
||||
return permissible_positions
|
||||
|
||||
|
||||
def is_complete(board: List[List[int]]) -> bool:
|
||||
def is_complete(board: list[list[int]]) -> bool:
|
||||
"""
|
||||
Check if the board (matrix) has been completely filled with non-zero values.
|
||||
|
||||
@ -47,7 +47,7 @@ def is_complete(board: List[List[int]]) -> bool:
|
||||
|
||||
|
||||
def open_knight_tour_helper(
|
||||
board: List[List[int]], pos: Tuple[int, int], curr: int
|
||||
board: list[list[int]], pos: tuple[int, int], curr: int
|
||||
) -> bool:
|
||||
"""
|
||||
Helper function to solve knight tour problem.
|
||||
@ -68,7 +68,7 @@ def open_knight_tour_helper(
|
||||
return False
|
||||
|
||||
|
||||
def open_knight_tour(n: int) -> List[List[int]]:
|
||||
def open_knight_tour(n: int) -> list[list[int]]:
|
||||
"""
|
||||
Find the solution for the knight tour problem for a board of size n. Raises
|
||||
ValueError if the tour cannot be performed for the given size.
|
||||
|
@ -7,12 +7,13 @@ if move is of maximizer return true else false
|
||||
leaves of game tree is stored in scores[]
|
||||
height is maximum height of Game tree
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List
|
||||
|
||||
|
||||
def minimax(
|
||||
depth: int, node_index: int, is_max: bool, scores: List[int], height: float
|
||||
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
|
||||
) -> int:
|
||||
"""
|
||||
>>> import math
|
||||
|
@ -7,12 +7,12 @@
|
||||
diagonal lines.
|
||||
|
||||
"""
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
solution = []
|
||||
|
||||
|
||||
def isSafe(board: List[List[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.
|
||||
@ -40,7 +40,7 @@ def isSafe(board: List[List[int]], row: int, column: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def solve(board: List[List[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
|
||||
@ -70,7 +70,7 @@ def solve(board: List[List[int]], row: int) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def printboard(board: List[List[int]]) -> None:
|
||||
def printboard(board: list[list[int]]) -> None:
|
||||
"""
|
||||
Prints the boards that have a successful combination.
|
||||
"""
|
||||
|
@ -75,14 +75,14 @@ Applying this two formulas we can check if a queen in some position is being att
|
||||
for another one or vice versa.
|
||||
|
||||
"""
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def depth_first_search(
|
||||
possible_board: List[int],
|
||||
diagonal_right_collisions: List[int],
|
||||
diagonal_left_collisions: List[int],
|
||||
boards: List[List[str]],
|
||||
possible_board: list[int],
|
||||
diagonal_right_collisions: list[int],
|
||||
diagonal_left_collisions: list[int],
|
||||
boards: list[list[str]],
|
||||
n: int,
|
||||
) -> None:
|
||||
"""
|
||||
@ -139,7 +139,7 @@ def depth_first_search(
|
||||
|
||||
|
||||
def n_queens_solution(n: int) -> None:
|
||||
boards: List[List[str]] = []
|
||||
boards: list[list[str]] = []
|
||||
depth_first_search([], [], [], boards, n)
|
||||
|
||||
# Print all the boards
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def solve_maze(maze: List[List[int]]) -> bool:
|
||||
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.
|
||||
@ -70,7 +70,7 @@ def solve_maze(maze: List[List[int]]) -> bool:
|
||||
return solved
|
||||
|
||||
|
||||
def run_maze(maze: List[List[int]], i: int, j: int, solutions: List[List[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.
|
||||
|
@ -9,9 +9,9 @@ function on the next column to see if it returns True. if yes, we
|
||||
have solved the puzzle. else, we backtrack and place another number
|
||||
in that cell and repeat this process.
|
||||
"""
|
||||
from typing import List, Optional, Tuple
|
||||
from __future__ import annotations
|
||||
|
||||
Matrix = List[List[int]]
|
||||
Matrix = list[list[int]]
|
||||
|
||||
# assigning initial values to the grid
|
||||
initial_grid: Matrix = [
|
||||
@ -59,7 +59,7 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def find_empty_location(grid: Matrix) -> Optional[Tuple[int, int]]:
|
||||
def find_empty_location(grid: Matrix) -> tuple[int, int] | None:
|
||||
"""
|
||||
This function finds an empty location so that we can assign a number
|
||||
for that particular row and column.
|
||||
@ -71,7 +71,7 @@ def find_empty_location(grid: Matrix) -> Optional[Tuple[int, int]]:
|
||||
return None
|
||||
|
||||
|
||||
def sudoku(grid: Matrix) -> Optional[Matrix]:
|
||||
def sudoku(grid: Matrix) -> Matrix | None:
|
||||
"""
|
||||
Takes a partially filled-in grid and attempts to assign values to
|
||||
all unassigned locations in such a way to meet the requirements
|
||||
|
@ -6,12 +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
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def generate_sum_of_subsets_soln(nums: List[int], max_sum: int) -> List[List[int]]:
|
||||
result: List[List[int]] = []
|
||||
path: List[int] = []
|
||||
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)
|
||||
@ -19,11 +19,11 @@ def generate_sum_of_subsets_soln(nums: List[int], max_sum: int) -> List[List[int
|
||||
|
||||
|
||||
def create_state_space_tree(
|
||||
nums: List[int],
|
||||
nums: list[int],
|
||||
max_sum: int,
|
||||
num_index: int,
|
||||
path: List[int],
|
||||
result: List[List[int]],
|
||||
path: list[int],
|
||||
result: list[list[int]],
|
||||
remaining_nums_sum: int,
|
||||
) -> None:
|
||||
"""
|
||||
|
Reference in New Issue
Block a user