Various ruff fixes (#12821)

This commit is contained in:
Christian Clauss
2025-07-06 00:35:29 +02:00
committed by GitHub
parent 7665ba5e25
commit cd3c3c3130
25 changed files with 69 additions and 70 deletions

View File

@ -39,12 +39,12 @@ https://www.geeksforgeeks.org/segment-tree-efficient-implementation/
from __future__ import annotations
from collections.abc import Callable
from typing import Any, Generic, TypeVar
from typing import Any, TypeVar
T = TypeVar("T")
class SegmentTree(Generic[T]):
class SegmentTree[T]:
def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None:
"""
Segment Tree constructor, it works just with commutative combiner.

View File

@ -10,14 +10,14 @@ https://www.youtube.com/watch?v=p33CVV29OG8
from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar
from typing import TypeVar
KEY = TypeVar("KEY")
VAL = TypeVar("VAL")
@dataclass(slots=True)
class _Item(Generic[KEY, VAL]):
class _Item[KEY, VAL]:
key: KEY
val: VAL

View File

@ -2,7 +2,7 @@ from __future__ import annotations
from abc import abstractmethod
from collections.abc import Iterable
from typing import Generic, Protocol, TypeVar
from typing import Protocol, TypeVar
class Comparable(Protocol):
@ -22,7 +22,7 @@ class Comparable(Protocol):
T = TypeVar("T", bound=Comparable)
class Heap(Generic[T]):
class Heap[T: Comparable]:
"""A Max Heap Implementation
>>> unsorted = [103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]

View File

@ -4,12 +4,12 @@ from __future__ import annotations
import random
from collections.abc import Iterable
from typing import Any, Generic, TypeVar
from typing import Any, TypeVar
T = TypeVar("T", bound=bool)
class RandomizedHeapNode(Generic[T]):
class RandomizedHeapNode[T: bool]:
"""
One node of the randomized heap. Contains the value and references to
two children.
@ -73,7 +73,7 @@ class RandomizedHeapNode(Generic[T]):
return root1
class RandomizedHeap(Generic[T]):
class RandomizedHeap[T: bool]:
"""
A data structure that allows inserting a new value and to pop the smallest
values. Both operations take O(logN) time where N is the size of the

View File

@ -3,12 +3,12 @@
from __future__ import annotations
from collections.abc import Iterable, Iterator
from typing import Any, Generic, TypeVar
from typing import Any, TypeVar
T = TypeVar("T", bound=bool)
class SkewNode(Generic[T]):
class SkewNode[T: bool]:
"""
One node of the skew heap. Contains the value and references to
two children.
@ -87,7 +87,7 @@ class SkewNode(Generic[T]):
return result
class SkewHeap(Generic[T]):
class SkewHeap[T: bool]:
"""
A data structure that allows inserting a new value and to pop the smallest
values. Both operations take O(logN) time where N is the size of the

View File

@ -7,13 +7,13 @@ from __future__ import annotations
from itertools import pairwise
from random import random
from typing import Generic, TypeVar
from typing import TypeVar
KT = TypeVar("KT")
VT = TypeVar("VT")
class Node(Generic[KT, VT]):
class Node[KT, VT]:
def __init__(self, key: KT | str = "root", value: VT | None = None):
self.key = key
self.value = value
@ -49,7 +49,7 @@ class Node(Generic[KT, VT]):
return len(self.forward)
class SkipList(Generic[KT, VT]):
class SkipList[KT, VT]:
def __init__(self, p: float = 0.5, max_level: int = 16):
self.head: Node[KT, VT] = Node[KT, VT]()
self.level = 0

View File

@ -1,13 +1,10 @@
"""Queue represented by a Python list"""
from collections.abc import Iterable
from typing import Generic, TypeVar
_T = TypeVar("_T")
class QueueByList(Generic[_T]):
def __init__(self, iterable: Iterable[_T] | None = None) -> None:
class QueueByList[T]:
def __init__(self, iterable: Iterable[T] | None = None) -> None:
"""
>>> QueueByList()
Queue(())
@ -16,7 +13,7 @@ class QueueByList(Generic[_T]):
>>> QueueByList((i**2 for i in range(1, 4)))
Queue((1, 4, 9))
"""
self.entries: list[_T] = list(iterable or [])
self.entries: list[T] = list(iterable or [])
def __len__(self) -> int:
"""
@ -58,7 +55,7 @@ class QueueByList(Generic[_T]):
return f"Queue({tuple(self.entries)})"
def put(self, item: _T) -> None:
def put(self, item: T) -> None:
"""Put `item` to the Queue
>>> queue = QueueByList()
@ -72,7 +69,7 @@ class QueueByList(Generic[_T]):
self.entries.append(item)
def get(self) -> _T:
def get(self) -> T:
"""
Get `item` from the Queue
@ -118,7 +115,7 @@ class QueueByList(Generic[_T]):
for _ in range(rotation):
put(get(0))
def get_front(self) -> _T:
def get_front(self) -> T:
"""Get the front item from the Queue
>>> queue = QueueByList((10, 20, 30))

View File

@ -1,13 +1,10 @@
"""Queue implementation using two stacks"""
from collections.abc import Iterable
from typing import Generic, TypeVar
_T = TypeVar("_T")
class QueueByTwoStacks(Generic[_T]):
def __init__(self, iterable: Iterable[_T] | None = None) -> None:
class QueueByTwoStacks[T]:
def __init__(self, iterable: Iterable[T] | None = None) -> None:
"""
>>> QueueByTwoStacks()
Queue(())
@ -16,8 +13,8 @@ class QueueByTwoStacks(Generic[_T]):
>>> QueueByTwoStacks((i**2 for i in range(1, 4)))
Queue((1, 4, 9))
"""
self._stack1: list[_T] = list(iterable or [])
self._stack2: list[_T] = []
self._stack1: list[T] = list(iterable or [])
self._stack2: list[T] = []
def __len__(self) -> int:
"""
@ -59,7 +56,7 @@ class QueueByTwoStacks(Generic[_T]):
"""
return f"Queue({tuple(self._stack2[::-1] + self._stack1)})"
def put(self, item: _T) -> None:
def put(self, item: T) -> None:
"""
Put `item` into the Queue
@ -74,7 +71,7 @@ class QueueByTwoStacks(Generic[_T]):
self._stack1.append(item)
def get(self) -> _T:
def get(self) -> T:
"""
Get `item` from the Queue

View File

@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Generic, TypeVar
from typing import TypeVar
T = TypeVar("T")
@ -13,7 +13,7 @@ class StackUnderflowError(BaseException):
pass
class Stack(Generic[T]):
class Stack[T]:
"""A stack is an abstract data type that serves as a collection of
elements with two principal operations: push() and pop(). push() adds an
element to the top of the stack, and pop() removes an element from the top

View File

@ -3,19 +3,19 @@
from __future__ import annotations
from typing import Generic, TypeVar
from typing import TypeVar
T = TypeVar("T")
class Node(Generic[T]):
class Node[T]:
def __init__(self, data: T):
self.data = data # Assign data
self.next: Node[T] | None = None # Initialize next as null
self.prev: Node[T] | None = None # Initialize prev as null
class Stack(Generic[T]):
class Stack[T]:
"""
>>> stack = Stack()
>>> stack.is_empty()

View File

@ -3,12 +3,12 @@
from __future__ import annotations
from collections.abc import Iterator
from typing import Generic, TypeVar
from typing import TypeVar
T = TypeVar("T")
class Node(Generic[T]):
class Node[T]:
def __init__(self, data: T):
self.data = data
self.next: Node[T] | None = None
@ -17,7 +17,7 @@ class Node(Generic[T]):
return f"{self.data}"
class LinkedStack(Generic[T]):
class LinkedStack[T]:
"""
Linked List Stack implementing push (to top),
pop (from top) and is_empty