Пример #1
0
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
Пример #2
0
class MyQueue:
    def __init__(self):
        self.stack1 = Stack(0)
        self.stack2 = Stack(0)
        self.stack1.pop()
        self.stack2.pop()
        self.use1 = True

    def add(self, value):
        if self.use1:
            self.stack1.push(value)
        else:
            self.stack2.push(value)

    def remove(self):
        if self.use1:
            self.use1 = not self.use1
            while (not self.stack1.isEmpty()):
                self.stack2.push(self.stack1.pop())
            return self.stack2.pop()
        else:
            self.use1 = not self.use1
            while (not self.stack2.isEmpty()):
                self.stack1.push(self.stack2.pop())
            return self.stack1.pop()
def postfixeval(list):
        values = Stack()

        for token in list:
            if token in "0123456789":
                values.push(int(token))

            else:
                if token in "()":
                    continue
                else:
                    op1 = values.pop()
                    op2 = values.pop()
                    result = calculate(token,op1,op2)
                    values.push(result)
        return values.pop()


        print values
Пример #4
0
 def reverse_level_order(self):
     to_traverse = [self.root]
     stack = Stack(100)
     while len(to_traverse) > 0:
         node = to_traverse[0]
         data = node.value
         stack.push(Node(data))
         if node.right:
             to_traverse.append(node.right)
         if node.left:
             to_traverse.append(node.left)
         to_traverse.pop(0)
     traversal = []
     for x in range(stack.size):
         traversal.append(stack.pop().get_value())
     return traversal
Пример #5
0
    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
Пример #6
0
    def dfsSize(self):
        if self.root is None:
            return 0

        stack = Stack()
        head = self.root
        if head is not None:
            stack.push(head)
        count = 0

        while len(stack) > 0:
            count += 1
            node = stack.pop()
            if node.left is not None:
                stack.push(node.left)
            if node.right is not None:
                stack.push(node.right)

        return count
Пример #7
0
def parChecker(string):
    s=Stack()
    balanced=True
    index=0
    while index<len(string) and balanced:
        symbol=string[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced=False
            else:
                top=s.pop()
                ## we have to see the top that we have removed matches the symbol
                if not matches(top,symbol):
                    balanced=False

        index+=1
    if balanced and s.isEmpty():
        return True
    else:
        return False
Пример #8
0
    def reverse_levelorder_print(self, start):
        if start is None:
            return

        queue = Queue()
        stack = Stack()
        queue.enqueue(start)

        traversal = ""
        while len(queue) > 0:
            node = queue.dequeue()

            stack.push(node)

            if node.right:
                queue.enqueue(node.right)
            if node.left:
                queue.enqueue(node.left)

        while len(stack) > 0:
            node = stack.pop()
            traversal += str(node.value) + "-"

        return traversal
Пример #9
0
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()