I still fail test case 4 & 5 in mathematics code challenge.is there anything I miss?


7th Feb 2023, 4:31 AM
Killiam Chances
Killiam Chances - avatar
5 Answers
+ 4
I think you miss any equation that uses multiple operations. If the inputs looked like this: 8 (2+2+2+2) (5/3) (3*3) Your code doesn't work, does it?
7th Feb 2023, 4:48 AM
Ausgrindtube - avatar
+ 2
I think your code is hard to read, I can't figure out what you're doing.. However, It's obvious you've broken the pemdas rule. Maybe your program is failing for this type of test due to how strangely eval function execute their argument... string concatenation do occurs result = 16 Expr = "2+2*3+8" eval function will likely solve it as eval(2+2*38) because of string concatenation I would suggest you after splitting all testcase by " ".. you should initiate operator precedence just like it is in real world. Expression with * and / should be on the same heirachy and at the top of + and -
7th Feb 2023, 7:12 AM
Mirielle - avatar
+ 1
Oh,I see thanks
7th Feb 2023, 5:02 AM
Killiam Chances
Killiam Chances - avatar
i believe that the code is trying to evaluate simple mathematical expressions and find if the result matches the given value, the problem with the code is that it only evaluates a single character operator between two numbers. To handle multiple characters of the expression and also the order of operations, you can use stack data structures and perform the calculation accordingly. Also, the code is using "result+=" to store the result, but this should be changed to simply "result =" since each calculation will overwrite the previous result, finally, the code should be handling cases for the expressions where it contains a wrong operator or the number format is incorrect, which may result in NumberFormatException.
7th Feb 2023, 5:25 AM
Do you know anything about evaluating that kind of expression?
7th Feb 2023, 6:50 AM
Killiam Chances
Killiam Chances - avatar