Skip to content

Commit cecf43d

Browse files
cclaussgithub-actions
and
github-actions
authored
Pyupgrade to Python 3.9 (TheAlgorithms#4718)
* Pyupgrade to Python 3.9 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 5d5831b commit cecf43d

File tree

142 files changed

+523
-530
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+523
-530
lines changed

DIRECTORY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@
545545
## Other
546546
* [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py)
547547
* [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py)
548-
* [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py)
548+
* [Davisb Putnamb Logemannb Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davisb_putnamb_logemannb_loveland.py)
549549
* [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py)
550550
* [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py)
551551
* [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
@@ -860,6 +860,7 @@
860860
* [Counting Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/counting_sort.py)
861861
* [Cycle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/cycle_sort.py)
862862
* [Double Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/double_sort.py)
863+
* [Dutch National Flag Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/dutch_national_flag_sort.py)
863864
* [Exchange Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/exchange_sort.py)
864865
* [External Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/external_sort.py)
865866
* [Gnome Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/gnome_sort.py)

arithmetic_analysis/in_static_equilibrium.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
22
Checks if a system of forces is in static equilibrium.
33
"""
4-
from typing import List
4+
from __future__ import annotations
55

66
from numpy import array, cos, cross, ndarray, radians, sin
77

88

99
def polar_force(
1010
magnitude: float, angle: float, radian_mode: bool = False
11-
) -> List[float]:
11+
) -> list[float]:
1212
"""
1313
Resolves force along rectangular components.
1414
(force, angle) => (force_x, force_y)

arithmetic_analysis/lu_decomposition.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Reference:
44
- https://en.wikipedia.org/wiki/LU_decomposition
55
"""
6-
from typing import Tuple
6+
from __future__ import annotations
77

88
import numpy as np
99

1010

11-
def lower_upper_decomposition(table: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
11+
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
1212
"""Lower-Upper (LU) Decomposition
1313
1414
Example:

arithmetic_analysis/newton_forward_interpolation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/
2+
from __future__ import annotations
23

34
import math
4-
from typing import List
55

66

77
# for calculating u value
@@ -22,7 +22,7 @@ def ucal(u: float, p: int) -> float:
2222

2323
def main() -> None:
2424
n = int(input("enter the numbers of values: "))
25-
y: List[List[float]] = []
25+
y: list[list[float]] = []
2626
for i in range(n):
2727
y.append([])
2828
for i in range(n):

arithmetic_analysis/newton_raphson.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
33
# The Newton-Raphson method (also known as Newton's method) is a way to
44
# quickly find a good approximation for the root of a real-valued function
5+
from __future__ import annotations
6+
57
from decimal import Decimal
68
from math import * # noqa: F401, F403
7-
from typing import Union
89

910
from sympy import diff
1011

1112

1213
def newton_raphson(
13-
func: str, a: Union[float, Decimal], precision: float = 10 ** -10
14+
func: str, a: float | Decimal, precision: float = 10 ** -10
1415
) -> float:
1516
"""Finds root from the point 'a' onwards by Newton-Raphson method
1617
>>> newton_raphson("sin(x)", 2)

backtracking/all_combinations.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
numbers out of 1 ... n. We use backtracking to solve this problem.
44
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
55
"""
6-
from typing import List
6+
from __future__ import annotations
77

88

9-
def generate_all_combinations(n: int, k: int) -> List[List[int]]:
9+
def generate_all_combinations(n: int, k: int) -> list[list[int]]:
1010
"""
1111
>>> generate_all_combinations(n=4, k=2)
1212
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
1313
"""
1414

15-
result: List[List[int]] = []
15+
result: list[list[int]] = []
1616
create_all_state(1, n, k, [], result)
1717
return result
1818

@@ -21,8 +21,8 @@ def create_all_state(
2121
increment: int,
2222
total_number: int,
2323
level: int,
24-
current_list: List[int],
25-
total_list: List[List[int]],
24+
current_list: list[int],
25+
total_list: list[list[int]],
2626
) -> None:
2727
if level == 0:
2828
total_list.append(current_list[:])
@@ -34,7 +34,7 @@ def create_all_state(
3434
current_list.pop()
3535

3636

37-
def print_all_state(total_list: List[List[int]]) -> None:
37+
def print_all_state(total_list: list[list[int]]) -> None:
3838
for i in total_list:
3939
print(*i)
4040

backtracking/all_permutations.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
Time complexity: O(n! * n),
66
where n denotes the length of the given sequence.
77
"""
8-
from typing import List, Union
8+
from __future__ import annotations
99

1010

11-
def generate_all_permutations(sequence: List[Union[int, str]]) -> None:
11+
def generate_all_permutations(sequence: list[int | str]) -> None:
1212
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
1313

1414

1515
def create_state_space_tree(
16-
sequence: List[Union[int, str]],
17-
current_sequence: List[Union[int, str]],
16+
sequence: list[int | str],
17+
current_sequence: list[int | str],
1818
index: int,
19-
index_used: List[int],
19+
index_used: list[int],
2020
) -> None:
2121
"""
2222
Creates a state space tree to iterate through each branch using DFS.
@@ -44,8 +44,8 @@ def create_state_space_tree(
4444
sequence = list(map(int, input().split()))
4545
"""
4646

47-
sequence: List[Union[int, str]] = [3, 1, 2, 4]
47+
sequence: list[int | str] = [3, 1, 2, 4]
4848
generate_all_permutations(sequence)
4949

50-
sequence_2: List[Union[int, str]] = ["A", "B", "C"]
50+
sequence_2: list[int | str] = ["A", "B", "C"]
5151
generate_all_permutations(sequence_2)

backtracking/all_subsequences.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
Time complexity: O(2^n),
66
where n denotes the length of the given sequence.
77
"""
8-
from typing import Any, List
8+
from __future__ import annotations
99

10+
from typing import Any
1011

11-
def generate_all_subsequences(sequence: List[Any]) -> None:
12+
13+
def generate_all_subsequences(sequence: list[Any]) -> None:
1214
create_state_space_tree(sequence, [], 0)
1315

1416

1517
def create_state_space_tree(
16-
sequence: List[Any], current_subsequence: List[Any], index: int
18+
sequence: list[Any], current_subsequence: list[Any], index: int
1719
) -> None:
1820
"""
1921
Creates a state space tree to iterate through each branch using DFS.
@@ -32,7 +34,7 @@ def create_state_space_tree(
3234

3335

3436
if __name__ == "__main__":
35-
seq: List[Any] = [3, 1, 2, 4]
37+
seq: list[Any] = [3, 1, 2, 4]
3638
generate_all_subsequences(seq)
3739

3840
seq.clear()

backtracking/coloring.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
66
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
77
"""
8-
from typing import List
98

109

1110
def valid_coloring(
12-
neighbours: List[int], colored_vertices: List[int], color: int
11+
neighbours: list[int], colored_vertices: list[int], color: int
1312
) -> bool:
1413
"""
1514
For each neighbour check if coloring constraint is satisfied
@@ -35,7 +34,7 @@ def valid_coloring(
3534

3635

3736
def util_color(
38-
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
37+
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
3938
) -> bool:
4039
"""
4140
Pseudo-Code
@@ -86,7 +85,7 @@ def util_color(
8685
return False
8786

8887

89-
def color(graph: List[List[int]], max_colors: int) -> List[int]:
88+
def color(graph: list[list[int]], max_colors: int) -> list[int]:
9089
"""
9190
Wrapper function to call subroutine called util_color
9291
which will either return True or False.

backtracking/hamiltonian_cycle.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
77
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
88
"""
9-
from typing import List
109

1110

1211
def valid_connection(
13-
graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int]
12+
graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
1413
) -> bool:
1514
"""
1615
Checks whether it is possible to add next into path by validating 2 statements
@@ -47,7 +46,7 @@ def valid_connection(
4746
return not any(vertex == next_ver for vertex in path)
4847

4948

50-
def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
49+
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
5150
"""
5251
Pseudo-Code
5352
Base Case:
@@ -108,7 +107,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
108107
return False
109108

110109

111-
def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
110+
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
112111
r"""
113112
Wrapper function to call subroutine called util_hamilton_cycle,
114113
which will either return array of vertices indicating hamiltonian cycle

backtracking/knight_tour.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM
22

3-
from typing import List, Tuple
3+
from __future__ import annotations
44

55

6-
def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
6+
def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
77
"""
88
Find all the valid positions a knight can move to from the current position.
99
@@ -32,7 +32,7 @@ def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
3232
return permissible_positions
3333

3434

35-
def is_complete(board: List[List[int]]) -> bool:
35+
def is_complete(board: list[list[int]]) -> bool:
3636
"""
3737
Check if the board (matrix) has been completely filled with non-zero values.
3838
@@ -47,7 +47,7 @@ def is_complete(board: List[List[int]]) -> bool:
4747

4848

4949
def open_knight_tour_helper(
50-
board: List[List[int]], pos: Tuple[int, int], curr: int
50+
board: list[list[int]], pos: tuple[int, int], curr: int
5151
) -> bool:
5252
"""
5353
Helper function to solve knight tour problem.
@@ -68,7 +68,7 @@ def open_knight_tour_helper(
6868
return False
6969

7070

71-
def open_knight_tour(n: int) -> List[List[int]]:
71+
def open_knight_tour(n: int) -> list[list[int]]:
7272
"""
7373
Find the solution for the knight tour problem for a board of size n. Raises
7474
ValueError if the tour cannot be performed for the given size.

backtracking/minimax.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
leaves of game tree is stored in scores[]
88
height is maximum height of Game tree
99
"""
10+
from __future__ import annotations
11+
1012
import math
11-
from typing import List
1213

1314

1415
def minimax(
15-
depth: int, node_index: int, is_max: bool, scores: List[int], height: float
16+
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
1617
) -> int:
1718
"""
1819
>>> import math

backtracking/n_queens.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
diagonal lines.
88
99
"""
10-
from typing import List
10+
from __future__ import annotations
1111

1212
solution = []
1313

1414

15-
def isSafe(board: List[List[int]], row: int, column: int) -> bool:
15+
def isSafe(board: list[list[int]], row: int, column: int) -> bool:
1616
"""
1717
This function returns a boolean value True if it is safe to place a queen there
1818
considering the current state of the board.
@@ -40,7 +40,7 @@ def isSafe(board: List[List[int]], row: int, column: int) -> bool:
4040
return True
4141

4242

43-
def solve(board: List[List[int]], row: int) -> bool:
43+
def solve(board: list[list[int]], row: int) -> bool:
4444
"""
4545
It creates a state space tree and calls the safe function until it receives a
4646
False Boolean and terminates that branch and backtracks to the next
@@ -70,7 +70,7 @@ def solve(board: List[List[int]], row: int) -> bool:
7070
return False
7171

7272

73-
def printboard(board: List[List[int]]) -> None:
73+
def printboard(board: list[list[int]]) -> None:
7474
"""
7575
Prints the boards that have a successful combination.
7676
"""

backtracking/n_queens_math.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@
7575
for another one or vice versa.
7676
7777
"""
78-
from typing import List
78+
from __future__ import annotations
7979

8080

8181
def depth_first_search(
82-
possible_board: List[int],
83-
diagonal_right_collisions: List[int],
84-
diagonal_left_collisions: List[int],
85-
boards: List[List[str]],
82+
possible_board: list[int],
83+
diagonal_right_collisions: list[int],
84+
diagonal_left_collisions: list[int],
85+
boards: list[list[str]],
8686
n: int,
8787
) -> None:
8888
"""
@@ -139,7 +139,7 @@ def depth_first_search(
139139

140140

141141
def n_queens_solution(n: int) -> None:
142-
boards: List[List[str]] = []
142+
boards: list[list[str]] = []
143143
depth_first_search([], [], [], boards, n)
144144

145145
# Print all the boards

backtracking/rat_in_maze.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import List
1+
from __future__ import annotations
22

33

4-
def solve_maze(maze: List[List[int]]) -> bool:
4+
def solve_maze(maze: list[list[int]]) -> bool:
55
"""
66
This method solves the "rat in maze" problem.
77
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:
7070
return solved
7171

7272

73-
def run_maze(maze: List[List[int]], i: int, j: int, solutions: List[List[int]]) -> bool:
73+
def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool:
7474
"""
7575
This method is recursive starting from (i, j) and going in one of four directions:
7676
up, down, left, right.

0 commit comments

Comments
 (0)