mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-07 11:37:36 +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:
@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user