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)
def push(self, value): top = self.stack.peek() if top.get_current_capacity() > 1: top.push(value) else: self.stack.push(MyStack(self.capacity_per_stack)) self.stack.peek().push(value)
def __init__(self, degree): # This method is complete. self.degree = degree self.rootNode = BTreeNode(degree) # If time, file creation code, etc. self.nodes = {} self.stackOfNodes = MyStack() self.rootNode.setIndex(1) self.writeAt(1, self.rootNode) self.rootIndex = 1 self.freeIndex = 2
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
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)
"/": op.floordiv, "+": op.add, "-": op.sub, } def postfix_eval(postfix): """Return evaluated resultant of postfix""" for item in postfix.split(" "): if item.isdigit(): stack.push(item) else: temp_two = int(stack.pop()) temp_one = int(stack.pop()) stack.push(oper_dict[item](temp_one, temp_two)) return int(stack.top()) stack = MyStack() def test_cases(): """Sample test cases""" assert postfix_eval('45 3 11 * + 9 +') == 87 assert postfix_eval('3 4 * 2 5 + / 2 5 4 + * 2 * 36 -') == 0 assert postfix_eval('6') == 6 print("Test Success!") test_cases()
def __init__(self, capacity_per_stack): self.capacity_per_stack = capacity_per_stack self.stack = MyStack(100) self.stack.push(MyStack(capacity_per_stack))
def __init__(self, capacity): self.output_stack = MyStack(capacity) self.help_stack = MyStack(capacity) self.capacity = capacity self.current_capacity = capacity
def __init__(self): self.main_stack = MyStack()
def __init__(self): super(MinStack, self).__init__() self.local_mins = MyStack()