def buildParseTree(pexp): plist = pexp.split() pStack = Stack() eTree = BinaryTree('') pStack.push(eTree) currentTree = eTree for i in plist: if i == '(': currentTree.insertLeft('') pStack.push(currentTree) currentTree = currentTree.getLeftChild() elif i in ['+', '-', '*', '/']: currentTree.setRootVal(i) currentTree.insertRight('') pStack.push(currentTree) currentTree = currentTree.getRightChild() elif i == ')': currentTree = pStack.pop() elif i not in ['+', '-', '*', '/', ')']: try: currentTree.setRootVal(int(i)) parent = pStack.pop() currentTree = parent except ValueError: raise ValueError( "token ' {} ' is not a valid integer".format(i)) return eTree
def postfixEval(postfixExpr): operandstack = Stack() tokenlist = postfixExpr.split() for token in tokenlist: if token in '01234567891011121314151617181920': operandstack.push(int(token)) else: operand2 = operandstack.pop() operand1 = operandstack.pop() result = doMath(operand1, operand2, token) operandstack.push(result) return operandstack.pop()
def divideBy2(decNum): remstack = Stack() while decNum > 0: rem = decNum % 2 remstack.push(rem) decNum = decNum // 2 binstring = '' while not remstack.isEmpty(): binstring = binstring + str(remstack.pop()) return binstring
def baseConverter(decNum, base): digits = "0123456789ABCDEF" remstack = Stack() while decNum > 0: rem = decNum % base remstack.push(rem) decNum = decNum // base binstring = '' while not remstack.isEmpty(): binstring = binstring + digits[remstack.pop()] return binstring
def parCheck(symbolstring): s = Stack() balanced = True for index in range(len(symbolstring)): symbol = symbolstring[index] if symbol in "([{": s.push(symbol) elif symbol in ")]}": if s.isEmpty() == True: balanced = False break else: top = s.pop() if not matches(top, symbol): balanced = False break if balanced and s.isEmpty(): return True else: return False
def infixToPostfix(expr): prec = {'**': 4, '*': 3, '/': 3, '+': 2, '-': 2, '(': 1} opStack = Stack() postfixList = [] tokenList = expr.split() alpha = set(string.ascii_uppercase) 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: # must put not Stack.isEmpty() first to evaluate if there is a operation token or not 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)
return isPalindrome(word[1:-1]) # Convert integer to string of specified base def toStr(n, base): convertStr = '0123456789ABCDEF' if n < base: return convertStr[n] else: return toStr(n // base, base) + convertStr[n % base] # Convert integer to string of specified base # using a stack from BasicDS import Stack rStack = Stack() def toStr(n, base): convertStr = '0123456789ABCDEF' while n > 0: if n < base: rStack.push(convertStr[n]) else: rStack.push(convertStr[n % base]) n = n // base res = "" while not rStack.isEmpty(): res = res + rStack.pop() return res