def is_balanced(parens: str): """Checks to see if a string of parens are balanced""" stack = Stack() try: for paren in parens: if stack.is_empty() or (not stack.is_empty() and paren == stack.peek()): stack.push(paren) else: stack.pop() return stack.is_empty() except IndexError: return False
def dfsPrint(self, start): if start is None: return stack = Stack() stack.push(start) traversal = "" while len(stack) > 0: traversal += str(stack.peek().value) + "->" node = stack.pop() if node.left: stack.push(node.left) if node.right: stack.push(node.right) return traversal
class Stack(object): def __init__(self): self.ll = LinkedList() def isEmpty(self): return self.ll.isEmpty() def push(self, data): self.ll.addNode(Node(data)) def pop(self): self.ll.delEnd() def peek(self): return self.ll.returnEndElement() def size(self): return self.ll.size() s = Stack() s.push(133) s.push(24) s.push(123) s.push(12124) s.pop() print s.peek() print s.size()