Given a mathematical expression as a string, return an int computing the value of the expression. EX: 1+3-6 = -2
Sigiloso
def ops(char, a, b): if char == '+': return a + b; elif char == '-': return a - b; elif char == '*': return a*b; else: return a/b; def calculate(list, char): while char in list: for i in range(1, len(list)-1): if list[i] == char: list[i-1] = ops(char, list[i-1], list[i+1]) del list[i:i+2] print list break def parse_numbers(str): start = 0 tmp = 0 list_tmp = [] while tmp < len(str): if(str[tmp].isdigit() == True or str[tmp] == '.'): if(tmp == (len(str) - 1)): list_tmp.append(float((''.join(str[tmp])))) break; else: tmp += 1 else: list_tmp.append(float((''.join(str[start:tmp])))) list_tmp.append(str[tmp]) tmp += 1 start = tmp return list_tmp def main(): a = '5*7-3*4/3+5-2*4' # b=list(a) print a b = parse_numbers(a) print b calculate(b,'/') calculate(b,'*') calculate(b,'+') calculate(b,'-') print 'Value of a is %f' %(b[0]) if __name__ == "__main__": main()