Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions conversions/prefix_conversions_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions data_structures/binary_tree/treap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

from collections.abc import Iterator
from dataclasses import dataclass
from typing import Any

Check failure on line 5 in data_structures/linked_list/circular_linked_list.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F401)

data_structures/linked_list/circular_linked_list.py:5:20: F401 `typing.Any` imported but unused help: Remove unused import: `typing.Any`


@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:
Expand All @@ -43,19 +43,19 @@
"""
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:
Expand Down Expand Up @@ -86,25 +86,25 @@
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:
IndexError: If the list is empty.
"""
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:
Expand Down
Loading