Question

Has anyone in this forum attempted to solve the ACM programming problem http://acm.mipt.ru/judge/problems.pl?browse=yes&problem=024? It is one of the simpler problems in ACM MIPT and the goal is to evaluate an expression consisting of +, -, * and parentheses. Despite the apparent simplicity, I haven't been able to get my solution accepted, apparently because one of the test case expressions has an operator not stated in the problem. I even added support for division ('/') but that too didn't help. Any idea on what other operator needs to be supported? FYI, my program removes all whitespaces from the input before processing so that spaces shouldn't be a problem. Anything not stated in the problem but needs to be taken care of?

Était-ce utile?

La solution

You're being bitten by ruby's handling of strings and characters.

curr_ch = @input[i]

gives you an integer, for the input you get, the ASCII code of the character at index i of the input.

curr_ch == '('

for example compares that integer to the string "(", of course that fails. Also the regex matches fail because you pass them an integer where a string is expected.

Replacing all occurrences of some_var = @input[some_index] with some_var = @input[some_index...some_index+1] gives me a programme that seems to work (it works on a few test inputs I gave it). Probably someone who actually knows the quirks of ruby can give you a better fix.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top