Login Register


Silver Challenge (2) Simple Equation Parser filter_list
Author
Message
RE: Challenge (2) Simple Equation Parser #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!

Reply





Messages In This Thread
Challenge (2) Simple Equation Parser - by m0dem - 12-19-2015, 11:04 PM
RE: Challenge (2) Simple Equation Parser - by m0dem - 02-17-2016, 04:20 AM



Users browsing this thread: 1 Guest(s)