From 3d5951060e63bf605c13e10bf3123147a823b411 Mon Sep 17 00:00:00 2001 From: troj4n Date: Mon, 26 Apr 2021 13:19:32 +0530 Subject: [PATCH] [mypy] Add/fix type annotations for stacks in data structures --- .../stacks/evaluate_postfix_notations.py | 5 +++-- data_structures/stacks/next_greater_element.py | 3 ++- data_structures/stacks/stack.py | 15 ++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/data_structures/stacks/evaluate_postfix_notations.py b/data_structures/stacks/evaluate_postfix_notations.py index a03cb43bb020..388e6d8b0117 100644 --- a/data_structures/stacks/evaluate_postfix_notations.py +++ b/data_structures/stacks/evaluate_postfix_notations.py @@ -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", "*"]) @@ -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() diff --git a/data_structures/stacks/next_greater_element.py b/data_structures/stacks/next_greater_element.py index d8c7ed17317b..de732107ac50 100644 --- a/data_structures/stacks/next_greater_element.py +++ b/data_structures/stacks/next_greater_element.py @@ -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] @@ -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))): diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 276684e12184..0e35f6ba89c5 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -1,3 +1,4 @@ +from typing import List class StackOverflowError(BaseException): pass @@ -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: @@ -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: