mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +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:
@ -5,15 +5,16 @@ python3 -m doctest -v avl_tree.py
|
||||
For testing run:
|
||||
python avl_tree.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
import random
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any
|
||||
|
||||
|
||||
class my_queue:
|
||||
def __init__(self) -> None:
|
||||
self.data: List[Any] = []
|
||||
self.data: list[Any] = []
|
||||
self.head: int = 0
|
||||
self.tail: int = 0
|
||||
|
||||
@ -41,17 +42,17 @@ class my_queue:
|
||||
class my_node:
|
||||
def __init__(self, data: Any) -> None:
|
||||
self.data = data
|
||||
self.left: Optional[my_node] = None
|
||||
self.right: Optional[my_node] = None
|
||||
self.left: my_node | None = None
|
||||
self.right: my_node | None = None
|
||||
self.height: int = 1
|
||||
|
||||
def get_data(self) -> Any:
|
||||
return self.data
|
||||
|
||||
def get_left(self) -> Optional["my_node"]:
|
||||
def get_left(self) -> my_node | None:
|
||||
return self.left
|
||||
|
||||
def get_right(self) -> Optional["my_node"]:
|
||||
def get_right(self) -> my_node | None:
|
||||
return self.right
|
||||
|
||||
def get_height(self) -> int:
|
||||
@ -61,11 +62,11 @@ class my_node:
|
||||
self.data = data
|
||||
return
|
||||
|
||||
def set_left(self, node: Optional["my_node"]) -> None:
|
||||
def set_left(self, node: my_node | None) -> None:
|
||||
self.left = node
|
||||
return
|
||||
|
||||
def set_right(self, node: Optional["my_node"]) -> None:
|
||||
def set_right(self, node: my_node | None) -> None:
|
||||
self.right = node
|
||||
return
|
||||
|
||||
@ -74,7 +75,7 @@ class my_node:
|
||||
return
|
||||
|
||||
|
||||
def get_height(node: Optional["my_node"]) -> int:
|
||||
def get_height(node: my_node | None) -> int:
|
||||
if node is None:
|
||||
return 0
|
||||
return node.get_height()
|
||||
@ -149,7 +150,7 @@ def rl_rotation(node: my_node) -> my_node:
|
||||
return left_rotation(node)
|
||||
|
||||
|
||||
def insert_node(node: Optional["my_node"], data: Any) -> Optional["my_node"]:
|
||||
def insert_node(node: my_node | None, data: Any) -> my_node | None:
|
||||
if node is None:
|
||||
return my_node(data)
|
||||
if data < node.get_data():
|
||||
@ -197,7 +198,7 @@ def get_leftMost(root: my_node) -> Any:
|
||||
return root.get_data()
|
||||
|
||||
|
||||
def del_node(root: my_node, data: Any) -> Optional["my_node"]:
|
||||
def del_node(root: my_node, data: Any) -> my_node | None:
|
||||
left_child = root.get_left()
|
||||
right_child = root.get_right()
|
||||
if root.get_data() == data:
|
||||
@ -275,7 +276,7 @@ class AVLtree:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.root: Optional[my_node] = None
|
||||
self.root: my_node | None = None
|
||||
|
||||
def get_height(self) -> int:
|
||||
return get_height(self.root)
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
@ -8,11 +8,11 @@ class Node:
|
||||
|
||||
def __init__(self, data: int) -> None:
|
||||
self.data = data
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
|
||||
def display(tree: Node | None) -> None: # In Order traversal of the tree
|
||||
"""
|
||||
>>> root = Node(1)
|
||||
>>> root.left = Node(0)
|
||||
@ -30,7 +30,7 @@ def display(tree: Optional[Node]) -> None: # In Order traversal of the tree
|
||||
display(tree.right)
|
||||
|
||||
|
||||
def depth_of_tree(tree: Optional[Node]) -> int:
|
||||
def depth_of_tree(tree: Node | None) -> int:
|
||||
"""
|
||||
Recursive function that returns the depth of a binary tree.
|
||||
|
||||
|
@ -7,21 +7,23 @@ python -m unittest binary_search_tree_recursive.py
|
||||
To run an example:
|
||||
python binary_search_tree_recursive.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from typing import Iterator, Optional
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
class Node:
|
||||
def __init__(self, label: int, parent: Optional["Node"]) -> None:
|
||||
def __init__(self, label: int, parent: Node | None) -> None:
|
||||
self.label = label
|
||||
self.parent = parent
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
class BinarySearchTree:
|
||||
def __init__(self) -> None:
|
||||
self.root: Optional[Node] = None
|
||||
self.root: Node | None = None
|
||||
|
||||
def empty(self) -> None:
|
||||
"""
|
||||
@ -66,9 +68,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
self.root = self._put(self.root, label)
|
||||
|
||||
def _put(
|
||||
self, node: Optional[Node], label: int, parent: Optional[Node] = None
|
||||
) -> Node:
|
||||
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
|
||||
if node is None:
|
||||
node = Node(label, parent)
|
||||
else:
|
||||
@ -98,7 +98,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._search(self.root, label)
|
||||
|
||||
def _search(self, node: Optional[Node], label: int) -> Node:
|
||||
def _search(self, node: Node | None, label: int) -> Node:
|
||||
if node is None:
|
||||
raise Exception(f"Node with label {label} does not exist")
|
||||
else:
|
||||
@ -140,7 +140,7 @@ class BinarySearchTree:
|
||||
else:
|
||||
self._reassign_nodes(node, None)
|
||||
|
||||
def _reassign_nodes(self, node: Node, new_children: Optional[Node]) -> None:
|
||||
def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
|
||||
if new_children:
|
||||
new_children.parent = node.parent
|
||||
|
||||
@ -244,7 +244,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._inorder_traversal(self.root)
|
||||
|
||||
def _inorder_traversal(self, node: Optional[Node]) -> Iterator[Node]:
|
||||
def _inorder_traversal(self, node: Node | None) -> Iterator[Node]:
|
||||
if node is not None:
|
||||
yield from self._inorder_traversal(node.left)
|
||||
yield node
|
||||
@ -266,7 +266,7 @@ class BinarySearchTree:
|
||||
"""
|
||||
return self._preorder_traversal(self.root)
|
||||
|
||||
def _preorder_traversal(self, node: Optional[Node]) -> Iterator[Node]:
|
||||
def _preorder_traversal(self, node: Node | None) -> Iterator[Node]:
|
||||
if node is not None:
|
||||
yield node
|
||||
yield from self._preorder_traversal(node.left)
|
||||
|
@ -1,13 +1,14 @@
|
||||
# https://en.wikipedia.org/wiki/Tree_traversal
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
data: int
|
||||
left: Optional["Node"] = None
|
||||
right: Optional["Node"] = None
|
||||
left: Node | None = None
|
||||
right: Node | None = None
|
||||
|
||||
|
||||
def make_tree() -> Node:
|
||||
|
@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List, Union
|
||||
|
||||
|
||||
class SegmentTree:
|
||||
@ -38,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]
|
||||
@ -89,7 +88,7 @@ class SegmentTree:
|
||||
# query with O(lg n)
|
||||
def query(
|
||||
self, idx: int, left_element: int, right_element: int, a: int, b: int
|
||||
) -> Union[int, float]:
|
||||
) -> int | float:
|
||||
"""
|
||||
query(1, 1, size, a, b) for query max of [a,b]
|
||||
>>> A = [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
|
||||
|
@ -5,7 +5,7 @@ The rule for merging is that if two nodes overlap, then put the value sum of
|
||||
both nodes to the new value of the merged node. Otherwise, the NOT null node
|
||||
will be used as the node of new tree.
|
||||
"""
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
@ -15,11 +15,11 @@ class Node:
|
||||
|
||||
def __init__(self, value: int = 0) -> None:
|
||||
self.value = value
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
|
||||
def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node:
|
||||
def merge_two_binary_trees(tree1: Node | None, tree2: Node | None) -> Node:
|
||||
"""
|
||||
Returns root node of the merged tree.
|
||||
|
||||
@ -52,7 +52,7 @@ def merge_two_binary_trees(tree1: Optional[Node], tree2: Optional[Node]) -> Node
|
||||
return tree1
|
||||
|
||||
|
||||
def print_preorder(root: Optional[Node]) -> None:
|
||||
def print_preorder(root: Node | None) -> None:
|
||||
"""
|
||||
Print pre-order traversal of the tree.
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
python/black : true
|
||||
flake8 : passed
|
||||
"""
|
||||
from typing import Iterator, Optional
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
class RedBlackTree:
|
||||
@ -21,11 +23,11 @@ class RedBlackTree:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
label: Optional[int] = None,
|
||||
label: int | None = None,
|
||||
color: int = 0,
|
||||
parent: Optional["RedBlackTree"] = None,
|
||||
left: Optional["RedBlackTree"] = None,
|
||||
right: Optional["RedBlackTree"] = None,
|
||||
parent: RedBlackTree | None = None,
|
||||
left: RedBlackTree | None = None,
|
||||
right: RedBlackTree | None = None,
|
||||
) -> None:
|
||||
"""Initialize a new Red-Black Tree node with the given values:
|
||||
label: The value associated with this node
|
||||
@ -42,7 +44,7 @@ class RedBlackTree:
|
||||
|
||||
# Here are functions which are specific to red-black trees
|
||||
|
||||
def rotate_left(self) -> "RedBlackTree":
|
||||
def rotate_left(self) -> RedBlackTree:
|
||||
"""Rotate the subtree rooted at this node to the left and
|
||||
returns the new root to this subtree.
|
||||
Performing one rotation can be done in O(1).
|
||||
@ -62,7 +64,7 @@ class RedBlackTree:
|
||||
right.parent = parent
|
||||
return right
|
||||
|
||||
def rotate_right(self) -> "RedBlackTree":
|
||||
def rotate_right(self) -> RedBlackTree:
|
||||
"""Rotate the subtree rooted at this node to the right and
|
||||
returns the new root to this subtree.
|
||||
Performing one rotation can be done in O(1).
|
||||
@ -82,7 +84,7 @@ class RedBlackTree:
|
||||
left.parent = parent
|
||||
return left
|
||||
|
||||
def insert(self, label: int) -> "RedBlackTree":
|
||||
def insert(self, label: int) -> RedBlackTree:
|
||||
"""Inserts label into the subtree rooted at self, performs any
|
||||
rotations necessary to maintain balance, and then returns the
|
||||
new root to this subtree (likely self).
|
||||
@ -139,7 +141,7 @@ class RedBlackTree:
|
||||
self.grandparent.color = 1
|
||||
self.grandparent._insert_repair()
|
||||
|
||||
def remove(self, label: int) -> "RedBlackTree":
|
||||
def remove(self, label: int) -> RedBlackTree:
|
||||
"""Remove label from this tree."""
|
||||
if self.label == label:
|
||||
if self.left and self.right:
|
||||
@ -337,7 +339,7 @@ class RedBlackTree:
|
||||
"""
|
||||
return self.search(label) is not None
|
||||
|
||||
def search(self, label: int) -> "RedBlackTree":
|
||||
def search(self, label: int) -> RedBlackTree:
|
||||
"""Search through the tree for label, returning its node if
|
||||
it's found, and None otherwise.
|
||||
This method is guaranteed to run in O(log(n)) time.
|
||||
@ -411,7 +413,7 @@ class RedBlackTree:
|
||||
return self.label
|
||||
|
||||
@property
|
||||
def grandparent(self) -> "RedBlackTree":
|
||||
def grandparent(self) -> RedBlackTree:
|
||||
"""Get the current node's grandparent, or None if it doesn't exist."""
|
||||
if self.parent is None:
|
||||
return None
|
||||
@ -419,7 +421,7 @@ class RedBlackTree:
|
||||
return self.parent.parent
|
||||
|
||||
@property
|
||||
def sibling(self) -> "RedBlackTree":
|
||||
def sibling(self) -> RedBlackTree:
|
||||
"""Get the current node's sibling, or None if it doesn't exist."""
|
||||
if self.parent is None:
|
||||
return None
|
||||
|
@ -1,9 +1,6 @@
|
||||
# flake8: noqa
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from random import random
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
class Node:
|
||||
@ -12,11 +9,11 @@ class Node:
|
||||
Treap is a binary tree by value and heap by priority
|
||||
"""
|
||||
|
||||
def __init__(self, value: Optional[int] = None):
|
||||
def __init__(self, value: int | None = None):
|
||||
self.value = value
|
||||
self.prior = random()
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
from pprint import pformat
|
||||
@ -35,7 +32,7 @@ class Node:
|
||||
return value + left + right
|
||||
|
||||
|
||||
def split(root: Optional[Node], value: int) -> Tuple[Optional[Node], Optional[Node]]:
|
||||
def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
|
||||
"""
|
||||
We split current tree into 2 trees with value:
|
||||
|
||||
@ -64,7 +61,7 @@ def split(root: Optional[Node], value: int) -> Tuple[Optional[Node], Optional[No
|
||||
return root, right
|
||||
|
||||
|
||||
def merge(left: Optional[Node], right: Optional[Node]) -> Optional[Node]:
|
||||
def merge(left: Node | None, right: Node | None) -> Node | None:
|
||||
"""
|
||||
We merge 2 trees into one.
|
||||
Note: all left tree's values must be less than all right tree's
|
||||
@ -86,7 +83,7 @@ def merge(left: Optional[Node], right: Optional[Node]) -> Optional[Node]:
|
||||
return right
|
||||
|
||||
|
||||
def insert(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
def insert(root: Node | None, value: int) -> Node | None:
|
||||
"""
|
||||
Insert element
|
||||
|
||||
@ -99,7 +96,7 @@ def insert(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
return merge(merge(left, node), right)
|
||||
|
||||
|
||||
def erase(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
def erase(root: Node | None, value: int) -> Node | None:
|
||||
"""
|
||||
Erase element
|
||||
|
||||
@ -112,7 +109,7 @@ def erase(root: Optional[Node], value: int) -> Optional[Node]:
|
||||
return merge(left, right)
|
||||
|
||||
|
||||
def inorder(root: Optional[Node]) -> None:
|
||||
def inorder(root: Node | None) -> None:
|
||||
"""
|
||||
Just recursive print of a tree
|
||||
"""
|
||||
@ -124,7 +121,7 @@ def inorder(root: Optional[Node]) -> None:
|
||||
inorder(root.right)
|
||||
|
||||
|
||||
def interactTreap(root: Optional[Node], args: str) -> Optional[Node]:
|
||||
def interactTreap(root: Node | None, args: str) -> Node | None:
|
||||
"""
|
||||
Commands:
|
||||
+ value to add value into treap
|
||||
|
@ -7,8 +7,7 @@ such as the with segment trees or fenwick trees. You can read more about them he
|
||||
2. https://www.youtube.com/watch?v=4aSv9PcecDw&t=811s
|
||||
3. https://www.youtube.com/watch?v=CybAgVF-MMc&t=1178s
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from __future__ import annotations
|
||||
|
||||
test_array = [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
|
||||
|
||||
@ -18,8 +17,8 @@ class Node:
|
||||
self.minn: int = -1
|
||||
self.maxx: int = -1
|
||||
self.map_left: list[int] = [-1] * length
|
||||
self.left: Optional[Node] = None
|
||||
self.right: Optional[Node] = None
|
||||
self.left: Node | None = None
|
||||
self.right: Node | None = None
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""
|
||||
|
@ -19,7 +19,7 @@ class HashTable:
|
||||
return self._keys
|
||||
|
||||
def balanced_factor(self):
|
||||
return sum([1 for slot in self.values if slot is not None]) / (
|
||||
return sum(1 for slot in self.values if slot is not None) / (
|
||||
self.size_table * self.charge_factor
|
||||
)
|
||||
|
||||
|
@ -14,7 +14,7 @@ class HashTableWithLinkedList(HashTable):
|
||||
|
||||
def balanced_factor(self):
|
||||
return (
|
||||
sum([self.charge_factor - len(slot) for slot in self.values])
|
||||
sum(self.charge_factor - len(slot) for slot in self.values)
|
||||
/ self.size_table
|
||||
* self.charge_factor
|
||||
)
|
||||
|
@ -1,4 +1,6 @@
|
||||
from typing import Iterable, List, Optional
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable
|
||||
|
||||
|
||||
class Heap:
|
||||
@ -25,19 +27,19 @@ class Heap:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.h: List[float] = []
|
||||
self.h: list[float] = []
|
||||
self.heap_size: int = 0
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return str(self.h)
|
||||
|
||||
def parent_index(self, child_idx: int) -> Optional[int]:
|
||||
def parent_index(self, child_idx: int) -> int | None:
|
||||
"""return the parent index of given child"""
|
||||
if child_idx > 0:
|
||||
return (child_idx - 1) // 2
|
||||
return None
|
||||
|
||||
def left_child_idx(self, parent_idx: int) -> Optional[int]:
|
||||
def left_child_idx(self, parent_idx: int) -> int | None:
|
||||
"""
|
||||
return the left child index if the left child exists.
|
||||
if not, return None.
|
||||
@ -47,7 +49,7 @@ class Heap:
|
||||
return left_child_index
|
||||
return None
|
||||
|
||||
def right_child_idx(self, parent_idx: int) -> Optional[int]:
|
||||
def right_child_idx(self, parent_idx: int) -> int | None:
|
||||
"""
|
||||
return the right child index if the right child exists.
|
||||
if not, return None.
|
||||
|
@ -3,7 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
from typing import Generic, Iterable, List, Optional, TypeVar
|
||||
from typing import Generic, Iterable, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
@ -16,8 +16,8 @@ class RandomizedHeapNode(Generic[T]):
|
||||
|
||||
def __init__(self, value: T) -> None:
|
||||
self._value: T = value
|
||||
self.left: Optional[RandomizedHeapNode[T]] = None
|
||||
self.right: Optional[RandomizedHeapNode[T]] = None
|
||||
self.left: RandomizedHeapNode[T] | None = None
|
||||
self.right: RandomizedHeapNode[T] | None = None
|
||||
|
||||
@property
|
||||
def value(self) -> T:
|
||||
@ -26,8 +26,8 @@ class RandomizedHeapNode(Generic[T]):
|
||||
|
||||
@staticmethod
|
||||
def merge(
|
||||
root1: Optional[RandomizedHeapNode[T]], root2: Optional[RandomizedHeapNode[T]]
|
||||
) -> Optional[RandomizedHeapNode[T]]:
|
||||
root1: RandomizedHeapNode[T] | None, root2: RandomizedHeapNode[T] | None
|
||||
) -> RandomizedHeapNode[T] | None:
|
||||
"""Merge 2 nodes together."""
|
||||
if not root1:
|
||||
return root2
|
||||
@ -69,13 +69,13 @@ class RandomizedHeap(Generic[T]):
|
||||
[-1, 0, 1]
|
||||
"""
|
||||
|
||||
def __init__(self, data: Optional[Iterable[T]] = ()) -> None:
|
||||
def __init__(self, data: Iterable[T] | None = ()) -> None:
|
||||
"""
|
||||
>>> rh = RandomizedHeap([3, 1, 3, 7])
|
||||
>>> rh.to_sorted_list()
|
||||
[1, 3, 3, 7]
|
||||
"""
|
||||
self._root: Optional[RandomizedHeapNode[T]] = None
|
||||
self._root: RandomizedHeapNode[T] | None = None
|
||||
for item in data:
|
||||
self.insert(item)
|
||||
|
||||
@ -151,7 +151,7 @@ class RandomizedHeap(Generic[T]):
|
||||
"""
|
||||
self._root = None
|
||||
|
||||
def to_sorted_list(self) -> List[T]:
|
||||
def to_sorted_list(self) -> list[T]:
|
||||
"""
|
||||
Returns sorted list containing all the values in the heap.
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Generic, Iterable, Iterator, Optional, TypeVar
|
||||
from typing import Generic, Iterable, Iterator, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
@ -15,8 +15,8 @@ class SkewNode(Generic[T]):
|
||||
|
||||
def __init__(self, value: T) -> None:
|
||||
self._value: T = value
|
||||
self.left: Optional[SkewNode[T]] = None
|
||||
self.right: Optional[SkewNode[T]] = None
|
||||
self.left: SkewNode[T] | None = None
|
||||
self.right: SkewNode[T] | None = None
|
||||
|
||||
@property
|
||||
def value(self) -> T:
|
||||
@ -25,8 +25,8 @@ class SkewNode(Generic[T]):
|
||||
|
||||
@staticmethod
|
||||
def merge(
|
||||
root1: Optional[SkewNode[T]], root2: Optional[SkewNode[T]]
|
||||
) -> Optional[SkewNode[T]]:
|
||||
root1: SkewNode[T] | None, root2: SkewNode[T] | None
|
||||
) -> SkewNode[T] | None:
|
||||
"""Merge 2 nodes together."""
|
||||
if not root1:
|
||||
return root2
|
||||
@ -69,13 +69,13 @@ class SkewHeap(Generic[T]):
|
||||
[-1, 0, 1]
|
||||
"""
|
||||
|
||||
def __init__(self, data: Optional[Iterable[T]] = ()) -> None:
|
||||
def __init__(self, data: Iterable[T] | None = ()) -> None:
|
||||
"""
|
||||
>>> sh = SkewHeap([3, 1, 3, 7])
|
||||
>>> list(sh)
|
||||
[1, 3, 3, 7]
|
||||
"""
|
||||
self._root: Optional[SkewNode[T]] = None
|
||||
self._root: SkewNode[T] | None = None
|
||||
for item in data:
|
||||
self.insert(item)
|
||||
|
||||
|
@ -5,7 +5,6 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable, Iterator
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
test_data_odd = (3, 9, -11, 0, 7, 5, 1, -1)
|
||||
test_data_even = (4, 6, 2, 0, 8, 10, 3, -2)
|
||||
@ -14,12 +13,12 @@ test_data_even = (4, 6, 2, 0, 8, 10, 3, -2)
|
||||
@dataclass
|
||||
class Node:
|
||||
data: int
|
||||
next: Optional[Node]
|
||||
next: Node | None
|
||||
|
||||
|
||||
class SortedLinkedList:
|
||||
def __init__(self, ints: Iterable[int]) -> None:
|
||||
self.head: Optional[Node] = None
|
||||
self.head: Node | None = None
|
||||
for i in reversed(sorted(ints)):
|
||||
self.head = Node(i, self.head)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class Node:
|
||||
@ -16,7 +16,7 @@ class Node:
|
||||
return "->".join(string_rep)
|
||||
|
||||
|
||||
def make_linked_list(elements_list: List):
|
||||
def make_linked_list(elements_list: list):
|
||||
"""Creates a Linked List from the elements of the given sequence
|
||||
(list/tuple) and returns the head of the Linked List.
|
||||
>>> make_linked_list([])
|
||||
|
@ -2,11 +2,10 @@
|
||||
Based on "Skip Lists: A Probabilistic Alternative to Balanced Trees" by William Pugh
|
||||
https://epaperpress.com/sortsearch/download/skiplist.pdf
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from random import random
|
||||
from typing import Generic, Optional, TypeVar
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
KT = TypeVar("KT")
|
||||
VT = TypeVar("VT")
|
||||
@ -124,7 +123,7 @@ class SkipList(Generic[KT, VT]):
|
||||
|
||||
return level
|
||||
|
||||
def _locate_node(self, key) -> tuple[Optional[Node[KT, VT]], list[Node[KT, VT]]]:
|
||||
def _locate_node(self, key) -> tuple[Node[KT, VT] | None, list[Node[KT, VT]]]:
|
||||
"""
|
||||
:param key: Searched key,
|
||||
:return: Tuple with searched node (or None if given key is not present)
|
||||
@ -222,7 +221,7 @@ class SkipList(Generic[KT, VT]):
|
||||
else:
|
||||
update_node.forward[i] = new_node
|
||||
|
||||
def find(self, key: VT) -> Optional[VT]:
|
||||
def find(self, key: VT) -> VT | None:
|
||||
"""
|
||||
:param key: Search key.
|
||||
:return: Value associated with given key or None if given key is not present.
|
||||
|
@ -1,5 +1,3 @@
|
||||
from typing import Any, List
|
||||
|
||||
"""
|
||||
The Reverse Polish Nation also known as Polish postfix notation
|
||||
or simply postfix notation.
|
||||
@ -8,6 +6,9 @@ Classic examples of simple stack implementations
|
||||
Valid operators are +, -, *, /.
|
||||
Each operand may be an integer or another expression.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def evaluate_postfix(postfix_notation: list) -> int:
|
||||
@ -23,7 +24,7 @@ def evaluate_postfix(postfix_notation: list) -> int:
|
||||
return 0
|
||||
|
||||
operations = {"+", "-", "*", "/"}
|
||||
stack: List[Any] = []
|
||||
stack: list[Any] = []
|
||||
|
||||
for token in postfix_notation:
|
||||
if token in operations:
|
||||
|
@ -1,5 +1,7 @@
|
||||
""" A Stack using a linked list like structure """
|
||||
from typing import Any, Optional
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Node:
|
||||
@ -42,7 +44,7 @@ class LinkedStack:
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.top: Optional[Node] = None
|
||||
self.top: Node | None = None
|
||||
|
||||
def __iter__(self):
|
||||
node = self.top
|
||||
|
@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
class StackOverflowError(BaseException):
|
||||
@ -15,7 +15,7 @@ class Stack:
|
||||
"""
|
||||
|
||||
def __init__(self, limit: int = 10):
|
||||
self.stack: List[int] = []
|
||||
self.stack: list[int] = []
|
||||
self.limit = limit
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
|
Reference in New Issue
Block a user