def evalInfixStack(expression): """ Evaluate a fully parenthesized infix expression using a stack. This function evaluates fully parenthesized expressions involving single digit integers and the operators addition (+), subtraction (-), multiplication (*), integer division (/), and modular division (%). A BadExpressionError will be raised if the expression contains unrecognized characters, or is not well formed. Spaces and tab characters are ignored. Arguments: expression - a string containing a mathematical expression. Returns: an integer containing the value of the expression. """ tempchar1 = "" variables = Stack() tempchar2 = expression.split() characters = tempchar1.join(tempchar2) print characters for i in characters: if not realNum(i) and i not in "+-%*/()": raise BadExpressionError() else: if realNum(i) or i in "+-%*/": variables.push(i) if i is ')': var2 = variables.pop() op = variables.pop() var1 = variables.pop() value = evaluate(op, var1, var2) variables.push(value) return variables.peek()
print input for ch in input: if ch >= 'A' and ch <= 'Z': output += ch if ch == '(': stack.push(ch) if ch == ')': while not stack.isEmpty(): cur = stack.pop() if cur == "(": break output += cur if ch in ('+', '-', '*', '/'): while not stack.isEmpty(): if getpri(stack.peek()) >= getpri(ch): output += stack.pop() else: break stack.push(ch) #if not stack.isEmpty(): #print "stack: " #stack.show() while not stack.isEmpty(): output += stack.pop() print output
class RedBlackStack(): # Creates an empty red/black stack. def __init__(self): self.RBStack=Stack() # Returns true if the red/black stack is empty of false otherwise. def isEmpty(self): return (self.RBStack.isEmpty()) # Returns the number of items in the stack. def __len__(self): return len(self.RBStack) # Pushes a node that stores item and color onto top of stack. def push(self, item, color): tmpColor=color.lower() assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color" tmpNode=_Node(item,color) self.RBStack.push(tmpNode) # Removes the last node placed on top of stack based on input color, and returns item. # If color is None, removes last node placed on stack and returns item. def pop(self,color=None): assert(not self.RBStack.isEmpty()),"Stack is Empty" if (color==None): return self.RBStack.pop().item tmpColor=color.lower() assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color" if(tmpColor=='r' or tmpColor=='red'): tmpStack=Stack() while(not self.RBStack.isEmpty()): tmpNode=self.RBStack.pop() if (tmpNode.color=='r' or tmpNode.color=='red'): while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) return tmpNode.item break else: tmpStack.push(tmpNode) while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) assert(False),"Color item not found." else: tmpStack=Stack() while(not self.RBStack.isEmpty()): tmpNode=self.RBStack.pop() if (tmpNode.color=='b' or tmpNode.color=='black'): while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) return tmpNode.item break else: tmpStack.push(tmpNode) while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) assert(False),"Color not found." # Returns last item on stack based on color input without removing respected node. # If color is None, returns last item placed on stack. def peek(self,color=None): assert(not self.RBStack.isEmpty()),"Stack is Empty" if (color==None): return self.RBStack.peek().item tmpColor=color.lower() assert(tmpColor=='r' or tmpColor=='b' or tmpColor=='black' or tmpColor=='red'),"Invalid color" if(tmpColor=='r' or tmpColor=='red'): tmpStack=Stack() while(not self.RBStack.isEmpty()): tmpNode=self.RBStack.peek() if (tmpNode.color=='r' or tmpNode.color=='red'): while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) return tmpNode.item break else: tmpStack.push(self.RBStack.pop()) while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) assert(False),"Color item not found." else: tmpStack=Stack() while(not self.RBStack.isEmpty()): tmpNode=self.RBStack.peek() if (tmpNode.color=='b' or tmpNode.color=='black'): while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) return tmpNode.item break else: tmpStack.push(self.RBStack.pop()) while(not tmpStack.isEmpty()): self.RBStack.push(tmpStack.pop()) assert(False),"Color not found."