def infixToPostfix(infixString): order = {"*": 3, "/": 3, "+": 2, "-": 2, "(": 1} operatorStack = Stack() postfixString = [] infixList = infixString.split() print infixList for infixEle in infixList: if infixEle in "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789": print "append %s to the output string" % infixEle postfixString.append(infixEle) elif infixEle == "(": print "push ( to the stack" operatorStack.push(infixEle) elif infixEle == ")": print "pop operator until see (" topOper = operatorStack.pop() while topOper != "(": print "pop %s until see (" % topOper postfixString.append(topOper) topOper = operatorStack.pop() else: while (not operatorStack.isEmpty()) and (order[operatorStack.peek()] >= order[infixEle]): print "pop out %s before push %s in" % (operatorStack.peek(), infixEle) postfixString.append(operatorStack.pop()) print "push %s in the stack" % infixEle operatorStack.push(infixEle) while not operatorStack.isEmpty(): postfixString.append(operatorStack.pop()) return postfixString
def parChecker(parString): s = Stack() for par in parString: if par == "(": s.push(par) else: if s.isEmpty(): return False else: s.pop() return s.isEmpty()
def symChecker(symString): s = Stack() for sym in symString: if sym in "({[": s.push(sym) else: if s.isEmpty(): return False else: matSym = s.pop() if not matchChecker(matSym,sym): return False return s.isEmpty()
def DecimalToBinary(decNum): binString = "" binStack = Stack() while decNum > 0: binNum = decNum % 2 binStack.push(binNum) decNum = decNum // 2 while not binStack.isEmpty(): binString += str(binStack.pop()) return binString
def DecimalToBase(decNum, base): system = "0123456789ABCDEF" baseString = "" baseStack = Stack() while decNum > 0: baseNum = decNum % base baseStack.push(baseNum) decNum = decNum // base while not baseStack.isEmpty(): baseString += system[baseStack.pop()] return baseString