0
I would like to ask help from you guys. I failed 2 test cases which are hidden, I am having difficulty figuring it out.
Code Coach - Mathematics; level is hard. https://code.sololearn.com/cR0C1v0XfF5q/?ref=app
3 Answers
+ 1
n=15
exp="(-2+3) (5*3) (-2-2) (20/10)"
ev=[eval(i) for i in exp.split()]
if n in ev: print(ev.index(n))
else:print(None)
0
I believe the best way to approach this question is to use built in function which is eval() to evaluate each expression. For example, let say you have a list of expression
exps = ["(-2+3)", "(5*3)", "(-2-2)", "(20/10)"]
for exp in exps:
r = eval(exp)
print(f"{exp} = {r}")
code above will print the expression and its result. Basically, eval() will take a string and evaluate that string. so if you give it eval("print('Test 1 2 3')"), then it will print `Test 1 2 3` when you run the code.
Note that you don't have to remove the parenthesis in this case
0
Thank you guys!