#!/usr/bin/python # -*- coding: utf-8 -*- digits = ['0','1','2','3','4','5','6','7','8','9'] #using 'x' instead of '*' to have a possibility to compare operators  # The following is true # '+' < '/'  # '-' < '/' # '+' < 'x' # '-' < 'x' # but # '+' > '*' # '-' > '*'  operators = ['+','-','/','x']   def summ(a,b):      return a+b def sub(a,b):      return b-a def mul(a,b):       return a*b def div(a,b):      return b/a  def infixToPostfix(string): 	stack = []     	res = [] 	exp = list(string.replace(" ", "")) 	firstOperator = true 	while exp: 		c = exp.pop(0) 		if c == '*': c = 'x' 		number = c 		if c in digits : 			while c in digits and exp: 				c = exp.pop(0)  				if c in digits: number = number + c 			res.append(number) 		if c in operators and firstOperator: 			stack.append(c) 			firstOperator = false 		else 			if c in prio2Operators: 			stack.append(c) 				 		if c == '(': 			stack.append(c) 		if c == ')': 			while stack[-1] != '(': 				res.append(stack.pop()) 			stack.pop() 	#TBD if in stack something else than operators then error 	while stack: 		res.append(stack.pop()) 	return res   def calcExpression(string): 	stack = []       	exp = infixToPostfix(string) 	funcs = {'+':summ,'-':sub,'*':mul,'/':div} 	while exp: 		if exp[0] in operators: 		    stack.append(funcs[exp.pop(0)](stack.pop(),stack.pop()))  		else: 			stack.append(float(exp.pop(0)))  	return stack[0]   print infixToPostfix("1+3*2") print calcExpression("1+(3*2)-3")