I would like to ask help from you guys. I failed 2 test cases which are hidden, I am having difficulty figuring it out. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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

8th Sep 2022, 5:19 PM
Cris Tradio
Cris Tradio - avatar
3 ответов
+ 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)
8th Sep 2022, 6:25 PM
Oma Falk
Oma Falk - avatar
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
8th Sep 2022, 5:43 PM
Shahrull Fytri
Shahrull Fytri - avatar
0
Thank you guys!
9th Sep 2022, 12:18 AM
Cris Tradio
Cris Tradio - avatar