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
5 changes: 3 additions & 2 deletions data_structures/stacks/evaluate_postfix_notations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Each operand may be an integer or another expression.
"""


from typing import List
def evaluate_postfix(postfix_notation: list) -> int:
"""
>>> evaluate_postfix(["2", "1", "+", "3", "*"])
Expand All @@ -21,8 +21,9 @@ def evaluate_postfix(postfix_notation: list) -> int:
return 0

operations = {"+", "-", "*", "/"}
stack = []

stack: List[int]

for token in postfix_notation:
if token in operations:
b, a = stack.pop(), stack.pop()
Expand Down
3 changes: 2 additions & 1 deletion data_structures/stacks/next_greater_element.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
arr = [-10, -5, 0, 5, 5.1, 11, 13, 21, 3, 4, -21, -10, -5, -1, 0]
expect = [-5, 0, 5, 5.1, 11, 13, 21, -1, 4, -1, -10, -5, -1, 0, -1]

Expand Down Expand Up @@ -53,7 +54,7 @@ def next_greatest_element(arr: list) -> list:
>>> next_greatest_element(arr) == expect
True
"""
stack = []
stack: List[int]
result = [-1] * len(arr)

for index in reversed(range(len(arr))):
Expand Down
15 changes: 8 additions & 7 deletions data_structures/stacks/stack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List
class StackOverflowError(BaseException):
pass

Expand All @@ -10,9 +11,9 @@ class Stack:
Last In, First Out (LIFO).
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
"""

def __init__(self, limit: int = 10):
self.stack = []
self.stack: List[int]
self.limit = limit

def __bool__(self) -> bool:
Expand All @@ -22,28 +23,28 @@ def __str__(self) -> str:
return str(self.stack)

def push(self, data):
"""Push an element to the top of the stack."""
""" Push an element to the top of the stack."""
if len(self.stack) >= self.limit:
raise StackOverflowError
self.stack.append(data)

def pop(self):
"""Pop an element off of the top of the stack."""
""" Pop an element off of the top of the stack."""
return self.stack.pop()

def peek(self):
"""Peek at the top-most element of the stack."""
""" Peek at the top-most element of the stack."""
return self.stack[-1]

def is_empty(self) -> bool:
"""Check if a stack is empty."""
""" Check if a stack is empty."""
return not bool(self.stack)

def is_full(self) -> bool:
return self.size() == self.limit

def size(self) -> int:
"""Return the size of the stack."""
""" Return the size of the stack."""
return len(self.stack)

def __contains__(self, item) -> bool:
Expand Down