Skip to content

Commit 9200a2e

Browse files
cclaussgithub-actions
and
github-actions
authored
from __future__ import annotations (TheAlgorithms#2464)
* from __future__ import annotations * fixup! from __future__ import annotations * fixup! from __future__ import annotations * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 6e6a49d commit 9200a2e

File tree

72 files changed

+275
-250
lines changed

Some content is hidden

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

72 files changed

+275
-250
lines changed

.travis.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ jobs:
1414
- scripts/validate_filenames.py # no uppercase, no spaces, in a directory
1515
- pip install -r requirements.txt # fast fail on black, flake8, validate_filenames
1616
script:
17-
- mypy --ignore-missing-imports .
17+
- mypy --ignore-missing-imports . || true # https://github.com/python/mypy/issues/7907
1818
- pytest --doctest-modules --ignore=project_euler/ --durations=10 --cov-report=term-missing:skip-covered --cov=. .
1919
- name: Project Euler
2020
before_script: pip install pytest-cov
2121
script:
2222
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
23+
after_success:
24+
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
2325
notifications:
2426
webhooks: https://www.travisbuddy.com/
2527
on_success: never
26-
after_success:
27-
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md

arithmetic_analysis/in_static_equilibrium.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
mypy : passed
77
"""
88

9-
from typing import List
9+
from __future__ import annotations
1010

1111
from numpy import array, cos, cross, radians, sin # type: ignore
1212

1313

1414
def polar_force(
1515
magnitude: float, angle: float, radian_mode: bool = False
16-
) -> List[float]:
16+
) -> list[float]:
1717
"""
1818
Resolves force along rectangular components.
1919
(force, angle) => (force_x, force_y)

backtracking/coloring.py

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

1010

1111
def valid_coloring(
12-
neighbours: List[int], colored_vertices: List[int], color: int
12+
neighbours: list[int], colored_vertices: list[int], color: int
1313
) -> bool:
1414
"""
1515
For each neighbour check if coloring constraint is satisfied
@@ -35,7 +35,7 @@ def valid_coloring(
3535

3636

3737
def util_color(
38-
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
38+
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
3939
) -> bool:
4040
"""
4141
Pseudo-Code
@@ -86,7 +86,7 @@ def util_color(
8686
return False
8787

8888

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

backtracking/hamiltonian_cycle.py

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

1111

1212
def valid_connection(
13-
graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int]
13+
graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
1414
) -> bool:
1515
"""
1616
Checks whether it is possible to add next into path by validating 2 statements
@@ -47,7 +47,7 @@ def valid_connection(
4747
return not any(vertex == next_ver for vertex in path)
4848

4949

50-
def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
50+
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
5151
"""
5252
Pseudo-Code
5353
Base Case:
@@ -108,7 +108,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
108108
return False
109109

110110

111-
def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
111+
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
112112
r"""
113113
Wrapper function to call subroutine called util_hamilton_cycle,
114114
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], n: int) -> List[Tuple[int]]:
6+
def get_valid_pos(position: tuple[int], n: int) -> list[tuple[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], n: int) -> List[Tuple[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
@@ -46,7 +46,7 @@ def is_complete(board: List[List[int]]) -> bool:
4646
return not any(elem == 0 for row in board for elem in row)
4747

4848

49-
def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int) -> bool:
49+
def open_knight_tour_helper(board: list[list[int]], pos: tuple[int], curr: int) -> bool:
5050
"""
5151
Helper function to solve knight tour problem.
5252
"""
@@ -66,7 +66,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)
6666
return False
6767

6868

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

backtracking/n_queens_math.py

+5-5
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
"""

cellular_automata/one_dimensional.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
https://mathworld.wolfram.com/ElementaryCellularAutomaton.html
55
"""
66

7-
from typing import List
7+
from __future__ import annotations
88

99
from PIL import Image
1010

@@ -15,7 +15,7 @@
1515
# fmt: on
1616

1717

18-
def format_ruleset(ruleset: int) -> List[int]:
18+
def format_ruleset(ruleset: int) -> list[int]:
1919
"""
2020
>>> format_ruleset(11100)
2121
[0, 0, 0, 1, 1, 1, 0, 0]
@@ -27,7 +27,7 @@ def format_ruleset(ruleset: int) -> List[int]:
2727
return [int(c) for c in f"{ruleset:08}"[:8]]
2828

2929

30-
def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[int]:
30+
def new_generation(cells: list[list[int]], rule: list[int], time: int) -> list[int]:
3131
population = len(cells[0]) # 31
3232
next_generation = []
3333
for i in range(population):
@@ -41,7 +41,7 @@ def new_generation(cells: List[List[int]], rule: List[int], time: int) -> List[i
4141
return next_generation
4242

4343

44-
def generate_image(cells: List[List[int]]) -> Image.Image:
44+
def generate_image(cells: list[list[int]]) -> Image.Image:
4545
"""
4646
Convert the cells into a greyscale PIL.Image.Image and return it to the caller.
4747
>>> from random import random

ciphers/rsa_factorization.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
More readable source: https://www.di-mgt.com.au/rsa_factorize_n.html
88
large number can take minutes to factor, therefore are not included in doctest.
99
"""
10+
from __future__ import annotations
11+
1012
import math
1113
import random
12-
from typing import List
1314

1415

15-
def rsafactor(d: int, e: int, N: int) -> List[int]:
16+
def rsafactor(d: int, e: int, N: int) -> list[int]:
1617
"""
1718
This function returns the factors of N, where p*q=N
1819
Return: [p, q]

compression/burrows_wheeler.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
original character. The BWT is thus a "free" method of improving the efficiency
1111
of text compression algorithms, costing only some extra computation.
1212
"""
13-
from typing import Dict, List
13+
from __future__ import annotations
1414

1515

16-
def all_rotations(s: str) -> List[str]:
16+
def all_rotations(s: str) -> list[str]:
1717
"""
1818
:param s: The string that will be rotated len(s) times.
1919
:return: A list with the rotations.
@@ -43,7 +43,7 @@ def all_rotations(s: str) -> List[str]:
4343
return [s[i:] + s[:i] for i in range(len(s))]
4444

4545

46-
def bwt_transform(s: str) -> Dict:
46+
def bwt_transform(s: str) -> dict:
4747
"""
4848
:param s: The string that will be used at bwt algorithm
4949
:return: the string composed of the last char of each row of the ordered

data_structures/binary_tree/lazy_segment_tree.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
2+
13
import math
2-
from typing import List
34

45

56
class SegmentTree:
@@ -36,7 +37,7 @@ def right(self, idx: int) -> int:
3637
return idx * 2 + 1
3738

3839
def build(
39-
self, idx: int, left_element: int, right_element: int, A: List[int]
40+
self, idx: int, left_element: int, right_element: int, A: list[int]
4041
) -> None:
4142
if left_element == right_element:
4243
self.segment_tree[idx] = A[left_element - 1]

data_structures/binary_tree/lowest_common_ancestor.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# https://en.wikipedia.org/wiki/Lowest_common_ancestor
22
# https://en.wikipedia.org/wiki/Breadth-first_search
33

4+
from __future__ import annotations
5+
46
import queue
5-
from typing import Dict, List, Tuple
67

78

8-
def swap(a: int, b: int) -> Tuple[int, int]:
9+
def swap(a: int, b: int) -> tuple[int, int]:
910
"""
1011
Return a tuple (b, a) when given two integers a and b
1112
>>> swap(2,3)
@@ -21,7 +22,7 @@ def swap(a: int, b: int) -> Tuple[int, int]:
2122
return a, b
2223

2324

24-
def create_sparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
25+
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
2526
"""
2627
creating sparse table which saves each nodes 2^i-th parent
2728
"""
@@ -35,8 +36,8 @@ def create_sparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
3536

3637
# returns lca of node u,v
3738
def lowest_common_ancestor(
38-
u: int, v: int, level: List[int], parent: List[List[int]]
39-
) -> List[List[int]]:
39+
u: int, v: int, level: list[int], parent: list[list[int]]
40+
) -> list[list[int]]:
4041
# u must be deeper in the tree than v
4142
if level[u] < level[v]:
4243
u, v = swap(u, v)
@@ -57,12 +58,12 @@ def lowest_common_ancestor(
5758

5859
# runs a breadth first search from root node of the tree
5960
def breadth_first_search(
60-
level: List[int],
61-
parent: List[List[int]],
61+
level: list[int],
62+
parent: list[list[int]],
6263
max_node: int,
63-
graph: Dict[int, int],
64+
graph: dict[int, int],
6465
root=1,
65-
) -> Tuple[List[int], List[List[int]]]:
66+
) -> tuple[list[int], list[list[int]]]:
6667
"""
6768
sets every nodes direct parent
6869
parent of root node is set to 0

data_structures/binary_tree/non_recursive_segment_tree.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
>>> st.query(0, 2)
3636
[1, 2, 3]
3737
"""
38-
from typing import Callable, List, TypeVar
38+
from __future__ import annotations
39+
40+
from typing import Callable, TypeVar
3941

4042
T = TypeVar("T")
4143

4244

4345
class SegmentTree:
44-
def __init__(self, arr: List[T], fnc: Callable[[T, T], T]) -> None:
46+
def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None:
4547
"""
4648
Segment Tree constructor, it works just with commutative combiner.
4749
:param arr: list of elements for the segment tree

data_structures/binary_tree/treap.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# flake8: noqa
22

3+
from __future__ import annotations
4+
35
from random import random
4-
from typing import Tuple
56

67

78
class Node:
@@ -33,7 +34,7 @@ def __str__(self):
3334
return value + left + right
3435

3536

36-
def split(root: Node, value: int) -> Tuple[Node, Node]:
37+
def split(root: Node, value: int) -> tuple[Node, Node]:
3738
"""
3839
We split current tree into 2 trees with value:
3940

data_structures/linked_list/skip_list.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
https://epaperpress.com/sortsearch/download/skiplist.pdf
44
"""
55

6+
from __future__ import annotations
7+
68
from random import random
7-
from typing import Generic, List, Optional, Tuple, TypeVar
9+
from typing import Generic, Optional, TypeVar
810

911
KT = TypeVar("KT")
1012
VT = TypeVar("VT")
@@ -14,7 +16,7 @@ class Node(Generic[KT, VT]):
1416
def __init__(self, key: KT, value: VT):
1517
self.key = key
1618
self.value = value
17-
self.forward: List[Node[KT, VT]] = []
19+
self.forward: list[Node[KT, VT]] = []
1820

1921
def __repr__(self) -> str:
2022
"""
@@ -122,7 +124,7 @@ def random_level(self) -> int:
122124

123125
return level
124126

125-
def _locate_node(self, key) -> Tuple[Optional[Node[KT, VT]], List[Node[KT, VT]]]:
127+
def _locate_node(self, key) -> tuple[Optional[Node[KT, VT]], list[Node[KT, VT]]]:
126128
"""
127129
:param key: Searched key,
128130
:return: Tuple with searched node (or None if given key is not present)

0 commit comments

Comments
 (0)