From ff77a52d667f87269bf66b59d7371df05be8d63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20O=C4=9Fulcan=20Say=C4=B1n?= <47698920+iosayin@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:41:19 +0300 Subject: [PATCH] refactor(typing): modernize CircularLinkedList and Treap Node with generic types and clean conversions TypeVar --- conversions/prefix_conversions_string.py | 4 --- data_structures/binary_tree/treap.py | 8 +++--- .../linked_list/circular_linked_list.py | 28 +++++++++---------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/conversions/prefix_conversions_string.py b/conversions/prefix_conversions_string.py index c5fef49874ca..9083037bb212 100644 --- a/conversions/prefix_conversions_string.py +++ b/conversions/prefix_conversions_string.py @@ -11,10 +11,6 @@ from __future__ import annotations from enum import Enum, unique -from typing import TypeVar - -# Create a generic variable that can be 'Enum', or any subclass. -T = TypeVar("T", bound="Enum") @unique diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index 7f105328ccea..1cd06e205841 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -3,17 +3,17 @@ from random import random -class Node: +class Node[T]: """ Treap's node Treap is a binary tree by value and heap by priority """ - def __init__(self, value: int | None = None) -> None: + def __init__(self, value: T | None = None) -> None: self.value = value self.prior = random() - self.left: Node | None = None - self.right: Node | None = None + self.left: Node[T] | None = None + self.right: Node[T] | None = None def __repr__(self) -> str: from pprint import pformat diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index bb64441d4560..fab5f33fff15 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -6,17 +6,17 @@ @dataclass -class Node: - data: Any - next_node: Node | None = None +class Node[T]: + data: T + next_node: Node[T] | None = None @dataclass -class CircularLinkedList: - head: Node | None = None # Reference to the head (first node) - tail: Node | None = None # Reference to the tail (last node) +class CircularLinkedList[T]: + head: Node[T] | None = None # Reference to the head (first node) + tail: Node[T] | None = None # Reference to the tail (last node) - def __iter__(self) -> Iterator[Any]: + def __iter__(self) -> Iterator[T]: """ Iterate through all nodes in the Circular Linked List yielding their data. Yields: @@ -43,19 +43,19 @@ def __repr__(self) -> str: """ return "->".join(str(item) for item in iter(self)) - def insert_tail(self, data: Any) -> None: + def insert_tail(self, data: T) -> None: """ Insert a node with the given data at the end of the Circular Linked List. """ self.insert_nth(len(self), data) - def insert_head(self, data: Any) -> None: + def insert_head(self, data: T) -> None: """ Insert a node with the given data at the beginning of the Circular Linked List. """ self.insert_nth(0, data) - def insert_nth(self, index: int, data: Any) -> None: + def insert_nth(self, index: int, data: T) -> None: """ Insert the data of the node at the nth pos in the Circular Linked List. Args: @@ -86,7 +86,7 @@ def insert_nth(self, index: int, data: Any) -> None: if index == len(self) - 1: # Insert at the tail self.tail = new_node - def delete_front(self) -> Any: + def delete_front(self) -> T: """ Delete and return the data of the node at the front of the Circular Linked List. Raises: @@ -94,17 +94,17 @@ def delete_front(self) -> Any: """ return self.delete_nth(0) - def delete_tail(self) -> Any: + def delete_tail(self) -> T: """ Delete and return the data of the node at the end of the Circular Linked List. Returns: - Any: The data of the deleted node. + T: The data of the deleted node. Raises: IndexError: If the index is out of range. """ return self.delete_nth(len(self) - 1) - def delete_nth(self, index: int = 0) -> Any: + def delete_nth(self, index: int = 0) -> T: """ Delete and return the data of the node at the nth pos in Circular Linked List. Args: