def infixtoPostFix(infixExpression): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 opStack = Stack() postFixList = [] tokenList = infixExpression.split() for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postFixList.append(token) elif token == '(': opStack.push(token) elif token == ")": topToken = opStack.pop() while topToken != '(': postFixList.append(topToken) topToken = opStack.pop() else: while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]): postFixList.append(opStack.pop()) opStack.push(token) while not opStack.isEmpty(): postFixList.append(opStack.pop()) return " ".join(postFixList)
def postFixEvaluation(postfixExpression): opStack = Stack() tokenList = postfixExpression.split() for token in tokenList: if token not in "*+-/==": opStack.push(int(token)) else: operand2 = opStack.pop() operand1 = opStack.pop() result = doMath(token, operand1, operand2) opStack.push(result) return opStack.pop()
def revstring(mystr): s1 = Stack() reversStr = "" for i in mystr: s1.push(i) while not s1.isEmpty(): reversStr += s1.pop() return reversStr
def paraenthesisCheck(parenthesisSympbol): s = Stack() matched = True index = 0 while index < len(parenthesisSympbol) and matched: symbol = parenthesisSympbol[index] if symbol in "([{": s.push(symbol) else: if s.isEmpty(): matched = False else: top = s.pop() if not matches(top, symbol): matched = False index = index + 1 if matched and s.isEmpty(): return True else: return False
from pythond.basic.stack.Stack import Stack s = Stack() print(s.isEmpty()) s.push(4) s.push("dog") print(s.peek()) s.push(True) print(s.size()) print(s.isEmpty()) s.push(8.4) print(s.pop()) print(s.pop()) print(s.size()) def revstring(mystr): s1 = Stack() reversStr = "" for i in mystr: s1.push(i) while not s1.isEmpty(): reversStr += s1.pop() return reversStr # print(s1.items) print(revstring("apple"))