from __future__ import annotations (#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>
This commit is contained in:
Christian Clauss
2020-09-23 13:30:13 +02:00
committed by GitHub
parent 6e6a49d19f
commit 9200a2e543
72 changed files with 275 additions and 250 deletions

View File

@@ -1,11 +1,12 @@
# https://en.wikipedia.org/wiki/Lowest_common_ancestor
# https://en.wikipedia.org/wiki/Breadth-first_search
from __future__ import annotations
import queue
from typing import Dict, List, Tuple
def swap(a: int, b: int) -> Tuple[int, int]:
def swap(a: int, b: int) -> tuple[int, int]:
"""
Return a tuple (b, a) when given two integers a and b
>>> swap(2,3)
@@ -21,7 +22,7 @@ def swap(a: int, b: int) -> Tuple[int, int]:
return a, b
def create_sparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
"""
creating sparse table which saves each nodes 2^i-th parent
"""
@@ -35,8 +36,8 @@ def create_sparse(max_node: int, parent: List[List[int]]) -> List[List[int]]:
# returns lca of node u,v
def lowest_common_ancestor(
u: int, v: int, level: List[int], parent: List[List[int]]
) -> List[List[int]]:
u: int, v: int, level: list[int], parent: list[list[int]]
) -> list[list[int]]:
# u must be deeper in the tree than v
if level[u] < level[v]:
u, v = swap(u, v)
@@ -57,12 +58,12 @@ def lowest_common_ancestor(
# runs a breadth first search from root node of the tree
def breadth_first_search(
level: List[int],
parent: List[List[int]],
level: list[int],
parent: list[list[int]],
max_node: int,
graph: Dict[int, int],
graph: dict[int, int],
root=1,
) -> Tuple[List[int], List[List[int]]]:
) -> tuple[list[int], list[list[int]]]:
"""
sets every nodes direct parent
parent of root node is set to 0