0
I still fail test case 4 & 5 in mathematics code challenge.is there anything I miss?
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?
+ 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 -
+ 1
Oh,I see thanks
0
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.
0
Do you know anything about evaluating that kind of expression?