def checkSymbolBalance(input): symbolstack = Stack() balanced = False for symbol in input: if symbol in ["(", "{", "["]: symbolstack.push(symbol) else: if symbolstack.isEmpty(): balanced = False else: topSymbol = symbolstack.pop() print("topSymbol: {0} ,symbol: {1}".format(topSymbol, symbol)) if not matches(topSymbol, symbol): balanced = False else: balanced = True return balanced
def infixToPostfix(infixexpr): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 opStack = Stack() postfixList = [] tokenList = infixexpr.split() for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postfixList.append(token) print("postfixList: " + str(postfixList)) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfixList.append(topToken) topToken = opStack.pop() print("postfixList: " + str(postfixList)) else: while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]): postfixList.append(opStack.pop()) print("postfixList: " + str(postfixList)) opStack.push(token) while not opStack.isEmpty(): postfixList.append(opStack.pop()) print("postfixList: " + str(postfixList)) return " ".join(postfixList)