+ 1

Could you help me in finding the problem about my code?

(I have difficulty in achieving the following task of Sololearn -named Mathematics. Could you find which point I'm missing? It fails at two hidden samples.) Find which math expression matches the answer that you are given, if you have an integer answer, and a list of math expressions. Task: Test each math expression to find the first one that matches the answer that you are given. Input Format: Two inputs: an integer and a space separated string of math expressions. The following operations need to be supported: addition +, subtraction -, multiplication *, division /. An expression can include multiple operations. Output Format: A string that tells the index of the first math expression that matches. If there are no matches, output 'none'. Sample Input: 15 (2+100) (5*3) (14+1) Sample Output: index 1 MY CODE: https://code.sololearn.com/crfv99owcDJc/?ref=app

2nd Nov 2021, 9:30 AM
Pınar Ongan
Pınar Ongan - avatar
4 Réponses
+ 2
And you can use eval function to make easier and faster in calculations
2nd Nov 2021, 10:08 AM
Sousou
Sousou - avatar
+ 3
@Slick: Thanks for your reply. Answer to your question: The second input comes with expressions surrounded by parenthesis AND seperated by space. (It may include multiple parenthesis in only one expression, too.) @Sousou: Thanks a lot for your both answer. "The (line 50)-change" didn't work too, but I was unaware of the existence of the eval() function, it's great to learn it :) It seems increadibly useful in this example. Best wishes to both of you!
2nd Nov 2021, 11:18 AM
Pınar Ongan
Pınar Ongan - avatar
0
Idk if this will make is correct, but I tried; Try change line 50 to: if int(n) == int(nArr[l]): Or this: if n.replace("\r","") == nArr[l]:
2nd Nov 2021, 10:04 AM
Sousou
Sousou - avatar
0
https://code.sololearn.com/cbhjFCCw81ah/?ref=app Sample input: 15 2+100 5*3 14+1 Sample output: Index: 1 This just splits the input up, checks against the first given number. And does the second input take a single string with expressions seperated by spaces the way it's described? or does the second input come with expressions surrounded by parenthesis AND seperated by space.
2nd Nov 2021, 10:35 AM
Slick
Slick - avatar