def infixStringCheck(infixExpr): tokenList = " ".join(infixExpr).split() parString = "" s = Stack() for i in range(len(tokenList)): if tokenList[i] in "([{}])": parString += tokenList[i] tokenList[i] = " " else: s.push(tokenList[i]) if not parentheses_checker(parString): return False else: while not s.isEmpty(): temp = s.pop() if not ((temp in string.ascii_letters) or \ (temp in "0123456789")): return False else: if s.isEmpty(): return True else: if not s.peek() in "+-*/": return False else: s.pop()
def revExpr(expr): s = Stack() tokenList = " ".join(expr).split() for token in tokenList: if token == "(": s.push(")") elif token == ")": s.push("(") else: s.push(token) revString = "" while not s.isEmpty(): revString += s.pop() return revString
def parentheses_checker(par_string): s = Stack() isBalanced = True index = 0 while (index < len(par_string)) and (isBalanced): symbol = par_string[index] if symbol in ['(', '[', '{']: s.push(symbol) else: if (s.isEmpty()) or (not match(s.peek(), symbol)): isBalanced = False else: s.pop() index += 1 return (isBalanced) and (s.isEmpty())
def postfixEval(postfixExpr): operandStack = Stack() tokenList = " ".join(postfixExpr).split() for token in tokenList: if token in "0123456789": operandStack.push(int(token)) else: operand2 = operandStack.pop() operand1 = operandStack.pop() temp = doMath(token, operand1, operand2) operandStack.push(temp) return operandStack.pop()
def postfixToInfix(postfixExpr): operandStack = Stack() tokenList = " ".join(postfixExpr).split() for token in tokenList: if (token in string.ascii_letters) or \ (token in "0123456789"): operandStack.push(token) else: operand2 = operandStack.pop() operand1 = operandStack.pop() tempString = f"({operand1} {token} {operand2})" operandStack.push(tempString) return operandStack.pop()
def decimalToBaseDecimal(decNumber, base, precision): if (base <= 1) or (base > 16): raise RuntimeError("Inproper base value") digits = "0123456789ABCDEF" s = Stack() decNumber = float(decNumber) for i in range(precision): decNumber *= base intPart, decNumber = seperateParts(decNumber) s.push(intPart) baseString = '' while not s.isEmpty(): baseString += digits[s.pop()] return baseString[(precision-1):0:-1]
def decimalToBaseInt(decNumber, base): digits = "0123456789ABCDEF" if (base <= 1) or (base > 16): raise RuntimeError("Inproper base value") decNumber = int(decNumber) remstack = Stack() while decNumber > 0: rem = decNumber % base remstack.push(rem) decNumber = decNumber // base baseString = '' while not remstack.isEmpty(): baseString += digits[remstack.pop()] return baseString
def infixToPostfix(infixExpr): if not infixStringCheck(infixExpr): raise RuntimeError("Incorrect Infix Expression") operatorStack = Stack() operatorDic = {} operatorDic["*"] = 3 operatorDic["/"] = 3 operatorDic["+"] = 2 operatorDic["-"] = 2 operatorDic["("] = 1 postfixList = [] tokenList = " ".join(infixExpr).split() for token in tokenList: if (token in string.ascii_letters) or \ (token in "0123456789"): postfixList.append(token) elif token == "(": operatorStack.push(token) elif token == ")": while operatorStack.peek() != "(": postfixList.append(operatorStack.pop()) operatorStack.pop() else: while (not operatorStack.isEmpty()) and \ (operatorDic[operatorStack.peek()] >= operatorDic[token]): postfixList.append(operatorStack.pop()) operatorStack.push(token) while not operatorStack.isEmpty(): postfixList.append(operatorStack.pop()) return " ".join(postfixList)