Example #1
0
def infixConverter(equationString):
    numStack = Stack()
    numList = []
#     operatorValues = {"/":2, "*":2, "+":1, "-":1}
    
    for variable in equationString:
        try: 
            int(variable)
            numList.append(variable)
        except ValueError:
            if variable == "(":
                numStack.push(variable)
            elif variable == ")":
                while numStack.top() != "(":
                    numList.append(numStack.pop())
                numStack.pop()
            elif (variable == "*" or variable == "/"):
                if numStack.top() == "*" or numStack.top() == "/":
                    numList.append(numStack.pop())
                    numStack.push(variable)
                else:
                    numStack.push(variable)
            elif (variable == "+" or variable == "-"):
                numStack.push(variable)
    while numStack.length() != 0:
        numList.append(numStack.pop())
    s = " "
    print(s.join(numList)) 
Example #2
0
def reverse_queue(queue):
    """ Reverse a queue.

    Note that this will destroy the original queue.

    Args:
        queue - a queue

    Returns the reverse of the input queue.
    """
    outqueue = QueueV3()
    stack = Stack()
    while not queue.length() == 0:
        item = queue.dequeue()
        stack.push(item)
        print('dequeued', item)
    while not stack.length() == 0:
        item = stack.pop()
        outqueue.enqueue(item)
        print('enqueued', item)
    return outqueue
Example #3
0
def main():
    print('stackReverser\n')
    stack1 = Stack()
    alpha = ['a', 'b', 'c', 'd', 'e']
    alpha = [1, 2, 3]
    for i in range(len(alpha)):
        stack1.push(alpha[i])

    rev_stack = stackReverser(stack1)
    print(rev_stack)

    for i in range(stack1.length()):
        print(stack1.pop())

    for i in range(rev_stack.length()):
        print(rev_stack.pop())

    print(rev_stack)

    print('bracketChecker\n')
    test_string = '[he{}ll(ooo)oooooo(o)]'
    print(bracketChecker(test_string))
Example #4
0
def reverse_queue2(queue):
    """ Reverse a queue.

    This will finish with the original queue in its original state.

    Args:
        queue - a queue

    Returns the reverse of the input queue.
    """
    outqueue = QueueV3()
    stack = Stack()
    i = queue.length()
    for pos in range(i):
        item = queue.dequeue()
        stack.push(item)
        print('dequeued', item)
        queue.enqueue(item)
    while not stack.length() == 0:
        item = stack.pop()
        outqueue.enqueue(item)
        print('enqueued', item)
    return outqueue