class Queue:
    def __init__(self):
        self._enqueue = Stack()
        self._dequeue = Stack()

    def is_empty(self):
        return self._enqueue.is_empty() and self._dequeue.is_empty()

    def size(self):
        return self._dequeue.size() + self._enqueue.size()

    def enqueue(self, item):
        self._enqueue.push(item)

    def dequeue(self):
        if self._dequeue.is_empty():
            while not self._enqueue.is_empty():
                self._dequeue.push(self._enqueue.pop())
        return self._dequeue.pop()

    def stdout(self):
        print('_enqueue')
        self._enqueue.stdout()
        print('_dequeue')
        self._dequeue.stdout()
Exemplo n.º 2
0
class StackQueue:
    """
    Implementation of Queue using two Stacks.
    """
    def __init__(self, limit=10):
        self.limit = limit
        self.stack_one = Stack(limit)
        self.stack_two = Stack(limit)

    def enqueue(self, item):
        if self.isFull():
            raise QueueOverflowError("Cannot insert item in a full queue.")
        self.stack_one.push(item)

    def _move_item_to_second_stack(self):
        if self.stack_two.is_empty():
            while not self.stack_one.is_empty():
                self.stack_two.push(self.stack_one.pop())

    def dequeue(self):
        if self.isEmpty():
            raise QueueUnderflowError(
                "Cannot Remove an item from empty Queue.")
        self._move_item_to_second_stack()
        return self.stack_two.pop()

    def peek(self):
        self._move_item_to_second_stack()
        return self.stack_two.peek()

    def isEmpty(self):
        return self.stack_one.is_empty() and self.stack_two.is_empty()

    def isFull(self):
        return (self.stack_one.size() + self.stack_two.size()) >= self.limit
Exemplo n.º 3
0
def infix_to_postfix(infix_expr):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    op_stack = Stack()
    postfix_list = []
    token_list = infix_expr.split()

    for token in token_list:
        if token in "ABCDEFGHIJKLMNOPQRSTUWXYZ" or token in "0123456789":
            postfix_list.append(token)
        elif token == '(':
            op_stack.push(token)
        elif token == ')':
            top_token = op_stack.pop()

            while top_token != '(':
                postfix_list.append(top_token)
                top_token = op_stack.pop()

        else:
            while (not op_stack.is_empty()) and \
                    (prec[op_stack.peek()] >= prec[token]):
                postfix_list.append(op_stack.pop())
            op_stack.push(token)

    while not op_stack.is_empty():
        postfix_list.append(op_stack.pop())

    return " ".join(postfix_list)
Exemplo n.º 4
0
def reverse_list(arr):
    stack = Stack()
    reversed = []
    for item in arr:
        stack.push(item)
    while stack.size() > 0:
        reversed.append(stack.pop())
    return reversed
Exemplo n.º 5
0
def postfix_eval(postfix_expr):
    operand_stack = Stack()
    token_list = postfix_expr.split()

    for token in token_list:
        if token in "0123456789":
            operand_stack.push(int(token))
        else:
            operand2 = operand_stack.pop()
            operand1 = operand_stack.pop()
            result = do_math(token, operand1, operand2)
            operand_stack.push(result)
    return operand_stack.pop()
def depth_first_search(root, isItem):
    s = Stack()
    s.push(root)
    while not s.is_empty():
        node = s.pop()
        if isItem(node['val']):
            return True
        try:
            for child in node['children']:
                s.push(child)
        except KeyError:
            continue
    return False
Exemplo n.º 7
0
def cannot_be_valid(expr):
    close = Stack()

    for char in reversed(expr):
        if char == CLOSE:
            close.push(char)
            continue

        try:
            close.pop()
        # an opening paren was reached with no closing tags to the right of it, this _could_ be valid
        except IndexError:
            return False

    return not close.is_empty()
def evaluate(string):
    stack = Stack()  # Stack<string>
    for char in string:
        if char in OPERATIONS or char == '(':
            stack.push(char)
        if char == ')':
            digit = stack.pop()
            stack.pop()  # remove the open parenthesis
            handleDigit(stack, digit)
        if char.isdigit():
            handleDigit(stack, char)

        # ignore other characters

    return int(stack.pop())
def par_checker(symbol_string):
    s = Stack()
    balanced = True
    index = 0

    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        if symbol in "([{":
            s.push(symbol)
        else:
            if s.is_empty():
                balanced = False
            else:
                top = s.pop()
                if not matches(top, symbol):
                    balanced = False

        index = index + 1

    if balanced and s.is_empty():
        return True
    else:
        return False