data_structures.stacks.stack_tracking_min_max¶
Classes¶
Main stack implementation |
|
Object stored on the stack |
Module Contents¶
- class data_structures.stacks.stack_tracking_min_max.MinMaxStack(max_stack_size: int = 10)¶
Main stack implementation
- get_current_max() float¶
Get the highest value on the stack in constant time
>>> test_stack = MinMaxStack(3) >>> test_stack.push_value(-450.45) True >>> test_stack.push_value(450.45) True >>> test_stack.push_value(0) True >>> test_stack.get_current_max() 450.45 >>> test_stack.pop_value() 0 >>> test_stack.get_current_max() 450.45 >>> test_stack.pop_value() 450.45 >>> test_stack.get_current_max() -450.45 >>> test_stack.pop_value() -450.45 >>> test_stack.get_current_max() Stack is empty -inf
- get_current_min() float¶
Get the lowest value on the stack in constant time
>>> test_stack = MinMaxStack(3) >>> test_stack.push_value(123) True >>> test_stack.push_value(-123) True >>> test_stack.push_value(0) True >>> test_stack.get_current_min() -123 >>> test_stack.pop_value() 0 >>> test_stack.get_current_min() -123 >>> test_stack.pop_value() -123 >>> test_stack.get_current_min() 123 >>> test_stack.pop_value() 123 >>> test_stack.get_current_min() Stack is empty -inf
- pop_value() float¶
Remove the top value from the stack.
>>> test_stack = MinMaxStack() >>> test_stack.push_value(1) True >>> test_stack.push_value(2) True >>> test_stack.pop_value() 2 >>> test_stack.pop_value() 1 >>> test_stack.pop_value() Stack is empty -inf
- push_value(value: float) bool¶
Push new value on top of stack
>>> test_stack = MinMaxStack(3) >>> test_stack.push_value(1) True >>> test_stack.push_value(2) True >>> test_stack.push_value(3) True >>> test_stack.push_value(4) Traceback (most recent call last): ... Exception: Max stack size reached.
- stack_is_valid() bool¶
Validate stack is not empty
>>> test_stack = MinMaxStack(3) >>> test_stack.stack_is_valid() Stack is empty False >>> test_stack.push_value(0) True >>> test_stack.stack_is_valid() True
- max_size = 10¶