RE: Challenge (2) Simple Equation Parser 02-17-2016, 04:20 AM
#7
(02-16-2016, 05:45 PM)emad.shaaban92 Wrote:Code:def parse(equation) : operators = ["^", "*", "/", "%", "+", "-"] result = {} for op in operators[::-1] : if op in equation : result[op] = [ parse(equation[:equation.find(op)]), parse(equation[equation.find(op)+1:]) ] return result return equation.strip() print parse('3 + 4 * 5')
You can support more operators by adding them to the list
also it respect the mathematical order of precedence
Sweet!





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)