Computer Science Canada getting rid of useless stuff for reverse polish notation |
Author: | person [ Fri Dec 08, 2006 8:53 pm ] | ||
Post subject: | getting rid of useless stuff for reverse polish notation | ||
for reverse polish notation (before converting stuff into reverse polish notation), i need to convert an expression from this fromat "-3+(-5(+(+7-2)))" into"(0-3+(0-5((7-2))))" so that the useless + signs are canceled and a "0" is placed in front of a negative number this task seems easy enough, but the assignment requires me to create a method that performs the function and the method cannot be greater than 3 lines long here is my shot at it, it's 6 lines long (not counting the brackets and the return statement)
can anyone give me pointers on how to condense this into 3 lines?[/quote] |
Author: | Tony [ Fri Dec 08, 2006 9:24 pm ] |
Post subject: | |
did you loose an extra sign? shouldn't it be (0-3+(0-5+((7-2)))) possibly with reduction of extra brackets? Regular Expressions would probably come in handy to cut down the number of lines |
Author: | zylum [ Fri Dec 08, 2006 11:51 pm ] | ||
Post subject: | |||
i havent really taken a close look but using the ternary operator will reduce the inner portion of your loop to one line so the whole loop will be one line reducing the whole method to 3 (long) lines. as tony mentioned, regular expressions can probably shorten your code.
|