# Sinisterly

Full Version: Challenge (1) CLI Calculator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(12-16-2015, 03:56 PM)Nevermore Wrote: [ -> ]I just got bored in history, I'll take it seriously and finish it later.

Oh, that's no problem.
I really liked the way you did it btw.
Code:
```import operator as op ops={'+': op.add,'-': op.sub,'*': op.mul,'/': op.div,'^': op.pow,'%': op.mod} def parse(equation) :     operators = ["^", "*", "/", "%", "+", "-"]     result = {}     for op in operators[::-1] :         if op in equation :             result['operator'] = op             result['operands'] = [                 parse(equation[:equation.find(op)]),                 parse(equation[equation.find(op)+1:])             ]             return result     return equation.strip() def evaluate(equation):     parsed = parse(equation)     def _eval(parsed_eq) :         if type(parsed_eq) is str and parsed_eq.isdigit() :             return int(parsed_eq)         else :             operator = parsed_eq['operator']             operands = parsed_eq['operands']             return ops[operator](_eval(operands[0]),  _eval(operands[1]))          return _eval(parsed)      exp = '3 + 4 * 5' print parse(exp) print evaluate(exp)```
(02-16-2016, 06:27 PM)emad.shaaban92 Wrote: [ -> ]
Code:
```import operator as op ops={'+': op.add,'-': op.sub,'*': op.mul,'/': op.div,'^': op.pow,'%': op.mod} def parse(equation) :     operators = ["^", "*", "/", "%", "+", "-"]     result = {}     for op in operators[::-1] :         if op in equation :             result['operator'] = op             result['operands'] = [                 parse(equation[:equation.find(op)]),                 parse(equation[equation.find(op)+1:])             ]             return result     return equation.strip() def evaluate(equation):     parsed = parse(equation)     def _eval(parsed_eq) :         if type(parsed_eq) is str and parsed_eq.isdigit() :             return int(parsed_eq)         else :             operator = parsed_eq['operator']             operands = parsed_eq['operands']             return ops[operator](_eval(operands[0]),  _eval(operands[1]))          return _eval(parsed)      exp = '3 + 4 * 5' print parse(exp) print evaluate(exp)```

I like it!
Pages: 1 2