mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-28 03:48:38 +00:00
Ruff changes
This commit is contained in:
parent
885d58f019
commit
d9dea17695
@ -1,22 +1,30 @@
|
|||||||
"""
|
|
||||||
Python3 stack implementation that retrieves min/max elements of the stack in constant time
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Object stored on the stack
|
Python3 stack implementation that retrieves min/max
|
||||||
|
elements of the stack in constant time
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class StackData:
|
class StackData:
|
||||||
def __init__(self, current_value: float, min_value: float, max_value: float) -> None:
|
"""
|
||||||
|
Object stored on the stack
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, current_value: float, min_value: float
|
||||||
|
, max_value: float) -> None:
|
||||||
self.current_value = current_value
|
self.current_value = current_value
|
||||||
self.min_value = min_value
|
self.min_value = min_value
|
||||||
self.max_value = max_value
|
self.max_value = max_value
|
||||||
|
|
||||||
"""
|
|
||||||
Main stack implementation
|
|
||||||
"""
|
|
||||||
class MinMaxStack:
|
class MinMaxStack:
|
||||||
|
"""
|
||||||
|
Main stack implementation
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, max_stack_size: int = 10) -> None:
|
def __init__(self, max_stack_size: int = 10) -> None:
|
||||||
self.max_size = max_stack_size
|
self.max_size = max_stack_size
|
||||||
self.stack = []
|
self.stack = []
|
||||||
@ -35,12 +43,13 @@ class MinMaxStack:
|
|||||||
>>> test_stack.push_value(4)
|
>>> test_stack.push_value(4)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
Exception: Max stack size reached. Remove elements before adding new ones or increase stack size.
|
Exception: Max stack size reached. Remove elements before
|
||||||
|
adding new ones or increase stack size.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if len(self.stack) == self.max_size:
|
if len(self.stack) == self.max_size:
|
||||||
raise Exception("Max stack size reached. Remove elements before adding new ones or increase stack size.")
|
raise Exception("Max stack size reached.")
|
||||||
|
|
||||||
if len(self.stack) == 0:
|
if len(self.stack) == 0:
|
||||||
self.stack.append(StackData(value, value, value))
|
self.stack.append(StackData(value, value, value))
|
||||||
@ -80,7 +89,7 @@ class MinMaxStack:
|
|||||||
"""
|
"""
|
||||||
Get the highest value on the stack in constant time
|
Get the highest value on the stack in constant time
|
||||||
|
|
||||||
test_stack = MinMaxStack(3)
|
>>> test_stack = MinMaxStack(3)
|
||||||
>>> test_stack.push_value(-450.45)
|
>>> test_stack.push_value(-450.45)
|
||||||
True
|
True
|
||||||
>>> test_stack.push_value(450.45)
|
>>> test_stack.push_value(450.45)
|
||||||
@ -154,3 +163,4 @@ class MinMaxStack:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from doctest import testmod
|
from doctest import testmod
|
||||||
testmod()
|
testmod()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user