def infix_to_postfix(expression): postfix = [] priority = {'(': 1, '&': 2, '|': 2, '!': 2} operators = Stack() for token in expression: if token in OPERATORS: # Operators are added to the stack, but first the ones with a # higher priority are added to the result: while not operators.is_empty() and priority[token] <= priority[ operators.top()]: postfix.append(operators.pop()) operators.push(token) # Left parenthesis are added to the stack: elif token == '(': operators.push(token) # Operators between parenthesis are added from the stack to the result: elif token == ')': while operators.top() != '(': postfix.append(operators.pop()) operators.pop() # Pop the left parentheses from the stack. # Operands are added to the result: else: postfix.append(token) while not operators.is_empty(): # The remaining operators are added from the stack to the result: postfix.append(operators.pop()) return postfix
class QueueOutOfStack: """Implements queue of two stacks. Idea: 1st stack for input, 2nd for output. While enqueue is called, data is stored in the input stack Once dequeue is called it gets data from the output stack until it's empty. If dequeue is called on empty output stack, all data from input stack is flush to the output stack. """ def __init__(self): self.input_stack = Stack() self.output_stack = Stack() def is_empty(self): return self.input_stack.is_empty() and self.output_stack.is_empty() def enqueue(self, item): self.input_stack.push(item) def dequeue(self): if self.output_stack.is_empty(): while not self.input_stack.is_empty(): self.output_stack.push(self.input_stack.pop()) return self.output_stack.pop()
def all_nearest_smaller_values_v2(a): """Compute all nearest smaller values of array a using a Stack.""" r = [] s = Stack() for ix, x in enumerate(a): while not s.is_empty() and s.peek()[1] >= x: s.pop() if s.is_empty(): r.append((None, None)) # (-1, None) else: r.append(s.peek()) s.push((ix, x)) return r
def is_parentheses_balanced(input_string: str) -> bool: """Checks a string for balanced brackets of 3 different kinds: (),{},[]. Args: input_string: a string to be checked Returns: True if parenthesis are balanced, False in other case """ if input_string is None or not isinstance(input_string, str): raise ValueError('Incorrect input parameter! Shall be string') brackets_stack = Stack() par_dict = {'}': '{', ')': '(', ']': '['} for char in input_string: if char in par_dict.values(): brackets_stack.push(char) elif char in par_dict.keys(): last_element = brackets_stack.peek() if last_element == par_dict[char]: brackets_stack.pop() else: return False else: continue return brackets_stack.is_empty()
def test_pop_until_empty(): s = Stack() s.push("apple") s.push("banana") s.push("cucumber") s.pop() s.pop() s.pop() actual = s.is_empty() expected = True assert actual == expected
def depth_first_list(self): """returns a list created by traversing the tree in depth first manner""" li = [] stack = Stack("bst") stack.push(self.root) while not stack.is_empty(): node = stack.pop() li.append(node.data) if node.left: stack.push(node.left) if node.right: stack.push(node.right) return li
def is_bracket_sequence_valid(sequence: str) -> bool: brackets_map: dict = { "[": "]", "{": "}", "(": ")", } opening_brackets = {"[", "(", "{"} closing_brackets = {"]", ")", "}"} stack = Stack() for symbol in sequence: if symbol in opening_brackets: stack.push(symbol) elif symbol in closing_brackets: if stack.is_empty(): return False stack_top = stack.pop() if brackets_map[stack_top] != symbol: return False else: raise ValueError(f"Symbol {symbol} is not allowed") return stack.is_empty()
class TestStack(TestCase): def setUp(self): super().setUp() self.stack = Stack() def test_pop(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.pop(), 2) self.assertEqual(self.stack.pop(), 1) def test_pop_empty(self): self.assertIsNone(self.stack.pop()) def test_peek(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.peek(), 2) self.stack.pop() self.assertEqual(self.stack.peek(), 1) def test_size(self): self.stack.push(1) self.stack.push(2) self.assertEqual(self.stack.size(), 2) def test_is_empty(self): self.assertTrue(self.stack.is_empty()) self.stack.push(1) self.assertFalse(self.stack.is_empty()) self.stack.pop()
def is_balanced(symbols): """ >>> is_balanced('([{}])') True >>> is_balanced('([]{}())') True >>> is_balanced('((()))') True >>> is_balanced('(([{])') False >>> is_balanced('[}([){]') False >>> is_balanced('[') False """ if not isinstance(symbols, str): raise TypeError('symbols must be a string') if len(symbols) == 0: raise ValueError('symbols must have at least one symbol') stack = Stack() for symbol in symbols: if stack.is_empty(): stack.push(symbol) else: top_symbol = stack.peek() if allowed_symbols.get(top_symbol, None) == symbol: stack.pop() else: stack.push(symbol) if stack.is_empty(): return True return False
def route_exists(self, node1, node2): """ Returns whether a route exists between two nodes in the graph. """ stack = Stack() for node in self.get_nodes(): node.visited = False stack.push(node1) while not stack.is_empty(): node = stack.pop() if node: for child in node.get_children(): if not child.visited: if child is node2: return True else: stack.push(child) node.visited = True return False
class BSTInOrderIterator: """Iterates through the binary search tree in order.""" def __init__(self, root: BinaryTreeNode[K, I]) -> None: self.stack = Stack() self.stack.push(root) self.__push_left_nodes(root) def __iter__(self) -> 'BSTInOrderIterator': return self def __next__(self) -> I: if self.stack.is_empty(): raise StopIteration current = self.stack.pop() if current.right_child is not None: self.stack.push(current.right) self.__push_left_nodes(current.right_child) return current.item def __push_left_nodes(self, current: BinaryTreeNode[K, I]) -> None: if current.left_child is not None: self.stack.push(current.left_child) self.__push_left_nodes(current.left_child)
def reverse_with_stack(self) -> None: """Reverses singly linked list using stack. Idea: Stack reverses the order of the elements. So, if we put all nodes to stack and then extract them, We will have a reversed linked list Note: This algorithm is less effective, since it requires additional space as well as two iterations - put and pop Returns: None, all changes done in linked list Raises: IllegalListReversalAttempt """ if self.is_linked_list_cycled(self.head): raise IllegalListReversalAttempt("Cannot reverse the linked list") self._tail = self.head current = self.head stack = Stack() # put all elements to stack while current.next is not None: stack.push(current) current = current.next new_head = current # pop all elements from stack and link while not stack.is_empty(): previous = current current = stack.pop() previous.next = current current.next = None self.head = new_head
def test_is_empty(): stk = Stack(0) assert stk.is_empty() == True
def test_stack_is_empty(): stack = Stack(5) assert stack.is_empty() is True stack.push(1) assert stack.is_empty() is False