Polish Notation String Calculator
Challenge: Given an always grammatically correct string that represent numbers, parenthesis, and operators add, sub, mult, and div, perform the simplification on the string.
Example
The Idea: Using recursion with a stack. Use the stack to find matching parenthesis. That is, the moment you identify a closing parenthesis the stack will reveal the index of the closing parenthesis. At this point you know which part of the substring to simplify. To make this clean, map a string to an operator. Since things are always going to be grammatically correct, we can avoid trying to identify them. Similify the target operation which will be in the form of ( [op] [list of numbers] ). Replace the target operation with the simplification. Then recurse on the same string again with the new tokens. Eventually the tokens will all simplify to a single number which will signal the end of recursion calls and you can return the final answer.
Simplified Python Solution
Last updated