mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 02:13:15 +08:00
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:
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List
|
||||
|
||||
|
||||
class SegmentTree:
|
||||
@ -36,7 +37,7 @@ class SegmentTree:
|
||||
return idx * 2 + 1
|
||||
|
||||
def build(
|
||||
self, idx: int, left_element: int, right_element: int, A: List[int]
|
||||
self, idx: int, left_element: int, right_element: int, A: list[int]
|
||||
) -> None:
|
||||
if left_element == right_element:
|
||||
self.segment_tree[idx] = A[left_element - 1]
|
||||
|
@ -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
|
||||
|
@ -35,13 +35,15 @@ https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
|
||||
>>> st.query(0, 2)
|
||||
[1, 2, 3]
|
||||
"""
|
||||
from typing import Callable, List, TypeVar
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class SegmentTree:
|
||||
def __init__(self, arr: List[T], fnc: Callable[[T, T], T]) -> None:
|
||||
def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None:
|
||||
"""
|
||||
Segment Tree constructor, it works just with commutative combiner.
|
||||
:param arr: list of elements for the segment tree
|
||||
|
@ -1,7 +1,8 @@
|
||||
# flake8: noqa
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from random import random
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class Node:
|
||||
@ -33,7 +34,7 @@ class Node:
|
||||
return value + left + right
|
||||
|
||||
|
||||
def split(root: Node, value: int) -> Tuple[Node, Node]:
|
||||
def split(root: Node, value: int) -> tuple[Node, Node]:
|
||||
"""
|
||||
We split current tree into 2 trees with value:
|
||||
|
||||
|
Reference in New Issue
Block a user