class MaxStack(Stack): def __init__(self) -> None: super().__init__() self.max_stack = Stack() def peek(self): return super().peek() def push(self, item: Any): if self.max_stack.empty(): self.max_stack.push(item) else: self.max_stack.push(max(item, self.max_stack.peek())) super().push(item) def pop(self): self.max_stack.pop() return super().pop() def max(self): """ Получить максимальный элемент. Не извлекает его из стека. """ return self.max_stack.peek() def empty(self): return super().empty() def __len__(self): return super().__len__() def __bool__(self): return super().__bool__()
def convert_infix_to_prefix(infix_expression): reverse = reverse_string(infix_expression) operators = Stack() prefix_expr = [] ops = '(+-*/**)' expr_list = list(reverse) for token in expr_list: if token not in ops: prefix_expr.append(token) elif token == ')': operators.push(token) elif token == '(': while operators.peek() != ')': prefix_expr.append(operators.pop()) operators.pop() else: while (not operators.is_empty() ) and precedence[operators.peek()] > precedence[token]: prefix_expr.append(operators.pop()) operators.push(token) while not operators.is_empty(): prefix_expr.append(operators.pop()) return reverse_string(''.join(prefix_expr))
class TestStack(unittest.TestCase): def setUp(self): self._stack = Stack() def test_pop_from_empty_stack_raises_index_error(self): self.assertRaises(IndexError, self._stack.pop) def test_pop_from_empty_stack_which_had_items_raises_index_error(self): self._stack.push(1) self._stack.pop() self.assertRaises(IndexError, self._stack.pop) def test_pop_returns_items_in_order(self): self._stack.push(3) self._stack.push(2) self._stack.push(1) assert (self._stack.pop() == 1) assert (self._stack.pop() == 2) assert (self._stack.pop() == 3) def test_peek_returns_top_item(self): self._stack.push(2) self._stack.push(1) assert (self._stack.peek() == 1) def test_peek_leaves_item_on_stack(self): self._stack.push(1) assert (self._stack.peek() == 1) assert (self._stack.pop() == 1)
def test_pop(): my_stack = Stack() my_stack.push(0) my_stack.push(1) popped = my_stack.pop() assert popped == 1 assert my_stack.peek() == 0
def check_parentheses(symbol_string): s = Stack() balanced = True index = 0 matcher = { '}': '{', ']': '[', ')': '(', } while index < len(symbol_string) and balanced: symbol = symbol_string[index] if symbol in ['(', '[', '{']: s.push(symbol) elif symbol in [')', ']', '}']: if s.is_empty() or matcher[symbol] != s.peek(): balanced = False else: s.pop() index = index + 1 if balanced and s.is_empty(): return True else: return False
def convert_infix_to_postfix(infix_expr): op = Stack() postfix = [] infix = list(infix_expr) for index in range(len(infix)): if infix[index] in ops: while (not op.is_empty()) and precedence[op.peek()] >= precedence[infix[index]]: if op.peek() == '(': op.pop() else: postfix.append(op.pop()) if infix[index] != ')': op.push(infix[index]) elif infix[index] != ' ': postfix.append(infix[index]) while op.size() > 0: postfix.append(op.pop()) print(''.join(postfix))
def convert_infix_to_postfix_new(infix_expr): op = Stack() postfix = [] infix = list(infix_expr) for index in range(len(infix)): token = infix[index] if token not in ops: postfix.append(token) elif token == '(': op.push(token) elif token == ')': while op.peek() != '(': postfix.append(op.pop()) op.pop() else: while (not op.is_empty()) and precedence[op.peek()] >= precedence[infix[index]]: postfix.append(op.pop()) op.push(token) while not op.is_empty(): postfix.append(op.pop()) print(''.join(postfix))
def test_peek(): my_stack = Stack() my_stack.push(0) my_stack.push(1) assert my_stack.peek() == 1
def test_stack_peek(given, peek): stack = Stack(given) assert stack.peek().value == peek