class MyQueue(object):
    def __init__(self, capacity):
        self.output_stack = MyStack(capacity)
        self.help_stack = MyStack(capacity)
        self.capacity = capacity
        self.current_capacity = capacity

    def __str__(self):
        output = "Queue: " + str(self.output_stack) + "\n"
        output += "Helper: " + str(self.help_stack)
        return output

    def add(self, value):
        if self.current_capacity > 0:
            if self.output_stack.is_empty() == True:
                self.output_stack.push(value)
            else:
                while self.output_stack.is_empty() == False:
                    self.help_stack.push(self.output_stack.pop())
                self.output_stack.push(value)
                self.current_capacity -= 1
                while self.help_stack.is_empty() == False:
                    self.output_stack.push(self.help_stack.pop())
        else:
            raise QueueOverFlow

    def remove(self):
        try:
            return self.output_stack.pop()
        except:
            raise EmptyQueueExeption
Пример #2
0
def infix_to_prefix(infix):
    """Converts given infix expression to postfix expression
       using Shunting Yard Algorithm and converts that postfix to prefix"""

    if infix == "":
        return ""
    #stack to temporarily store operators and paranthesis
    stack = MyStack(size=len(infix) + 1)
    postfix = []  # a list to store postifix expression

    for char in infix:
        if is_operand(char):
            postfix.append(char)
        elif char not in ['(', ')']:
            while not stack.is_empty() and precedence(char) <= precedence(
                    stack.top()):
                #Add elements from stack until stack is not empty and precedence of \n
                #char is less than the top most stack element
                postfix.append(stack.pop())
            stack.push(char)
        elif char == "(":
            stack.push(char)
        elif char == ")":
            while not stack.is_empty() and stack.top() != "(":
                postfix.append(stack.pop())
            if stack.top() != "(":
                raise ValueError("Parathesis Mismatch!")
            stack.pop()
    while not stack.is_empty():
        # pop out and add all existing elements from stack and add in onto postfix
        postfix.append(stack.pop())
    stack.clear_stack()
    # returns prefix string after converting the postfix passed to convert_to_prefix
    return convert_to_prefix(postfix)
Пример #3
0
    def dequeue(self):
        # Write your code here
        temp_stack = MyStack()
        
        while not self.main_stack.size() == 1:
            temp_stack.push(self.main_stack.pop())
        
        popped_item = self.main_stack.pop()

        while not temp_stack.is_empty():
            self.main_stack.push(temp_stack.pop())

        return popped_item
Пример #4
0
def infix_to_postfix(infix):
    """Converts given infix expression to postfix expression
       using Shunting Yard Algorithm"""
    
    #stack to temporarily store operators and paranthesis
    stack = MyStack(size= len(infix)+1) 
    postfix = []                      # a list to store postifix expression
    
    # Returns True if char is an operand
    is_operand = lambda char: char.isalpha() or char.isnumeric()

    # Returns the precedence of char from PRIORITY dict"""
    PRIORITY = {"+": 1, "-": 1, "*": 2, "/": 2, "%": 2, "^": 3}
    precedence = lambda char: PRIORITY[char] if char in PRIORITY else -1

    for char in infix:
        if is_operand(char):
            postfix.append(char)
        elif char not in ['(',')']:
            while not stack.is_empty() and precedence(char) <= precedence(stack.top()):
                #Add elements from stack until stack is not empty and precedence of \n
                #char is less than the top most stack element
                postfix.append(stack.pop())
            stack.push(char)
        elif char == "(":
            stack.push(char)
        elif char == ")":
            while not stack.is_empty() and stack.top() != "(":
                postfix.append(stack.pop())
            if stack.top() != "(":
                raise ValueError("Parathesis Mismatch!")
            stack.pop()
    while not stack.is_empty():
        # pop out and add all existing elements from stack and add in onto postfix
        postfix.append(stack.pop())
    return " ".join(postfix)