diff --git a/DIRECTORY.md b/DIRECTORY.md index 6fbd5e2cc1c5..88df541eaf6a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -126,6 +126,7 @@ * [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py) * [Octal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/octal_to_decimal.py) * [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py) + * [Pressure Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/pressure_conversions.py) * [Rgb Hsv Conversion](https://github.com/TheAlgorithms/Python/blob/master/conversions/rgb_hsv_conversion.py) * [Roman Numerals](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_numerals.py) * [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py) @@ -196,12 +197,12 @@ * [Evaluate Postfix Notations](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/evaluate_postfix_notations.py) * [Infix To Postfix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_postfix_conversion.py) * [Infix To Prefix Conversion](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/infix_to_prefix_conversion.py) - * [Linked Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/linked_stack.py) * [Next Greater Element](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/next_greater_element.py) * [Postfix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/postfix_evaluation.py) * [Prefix Evaluation](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/prefix_evaluation.py) * [Stack](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack.py) - * [Stack Using Dll](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_using_dll.py) + * [Stack With Doubly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_doubly_linked_list.py) + * [Stack With Singly Linked List](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stack_with_singly_linked_list.py) * [Stock Span Problem](https://github.com/TheAlgorithms/Python/blob/master/data_structures/stacks/stock_span_problem.py) * Trie * [Trie](https://github.com/TheAlgorithms/Python/blob/master/data_structures/trie/trie.py) @@ -858,6 +859,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py) * Problem 551 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) + * Problem 686 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_686/sol1.py) ## Quantum * [Deutsch Jozsa](https://github.com/TheAlgorithms/Python/blob/master/quantum/deutsch_jozsa.py) diff --git a/data_structures/stacks/stack_using_dll.py b/data_structures/stacks/stack_with_doubly_linked_list.py similarity index 100% rename from data_structures/stacks/stack_using_dll.py rename to data_structures/stacks/stack_with_doubly_linked_list.py diff --git a/data_structures/stacks/linked_stack.py b/data_structures/stacks/stack_with_singly_linked_list.py similarity index 83% rename from data_structures/stacks/linked_stack.py rename to data_structures/stacks/stack_with_singly_linked_list.py index 85b59a940e39..903ae39db4b5 100644 --- a/data_structures/stacks/linked_stack.py +++ b/data_structures/stacks/stack_with_singly_linked_list.py @@ -1,19 +1,22 @@ """ A Stack using a linked list like structure """ from __future__ import annotations -from typing import Any +from collections.abc import Iterator +from typing import Generic, TypeVar +T = TypeVar("T") -class Node: - def __init__(self, data): + +class Node(Generic[T]): + def __init__(self, data: T): self.data = data - self.next = None + self.next: Node[T] | None = None - def __str__(self): + def __str__(self) -> str: return f"{self.data}" -class LinkedStack: +class LinkedStack(Generic[T]): """ Linked List Stack implementing push (to top), pop (from top) and is_empty @@ -44,15 +47,15 @@ class LinkedStack: """ def __init__(self) -> None: - self.top: Node | None = None + self.top: Node[T] | None = None - def __iter__(self): + def __iter__(self) -> Iterator[T]: node = self.top while node: yield node.data node = node.next - def __str__(self): + def __str__(self) -> str: """ >>> stack = LinkedStack() >>> stack.push("c") @@ -63,7 +66,7 @@ def __str__(self): """ return "->".join([str(item) for item in self]) - def __len__(self): + def __len__(self) -> int: """ >>> stack = LinkedStack() >>> len(stack) == 0 @@ -87,7 +90,7 @@ def is_empty(self) -> bool: """ return self.top is None - def push(self, item: Any) -> None: + def push(self, item: T) -> None: """ >>> stack = LinkedStack() >>> stack.push("Python") @@ -101,7 +104,7 @@ def push(self, item: Any) -> None: node.next = self.top self.top = node - def pop(self) -> Any: + def pop(self) -> T: """ >>> stack = LinkedStack() >>> stack.pop() @@ -125,7 +128,7 @@ def pop(self) -> Any: self.top = self.top.next return pop_node.data - def peek(self) -> Any: + def peek(self) -> T: """ >>> stack = LinkedStack() >>> stack.push("Java")