In this problem, you are given a string representing a postfix expression (also known as Reverse Polish Notation). The expression contains integers and the operators +
, -
, *
, and /
, all separated by spaces. Your task is to evaluate the expression using a stack and return the result as a number.
For the input:
'3 4 + 2 * 7 /'
The evaluation would proceed as follows:
+
, pop 4 and 3, compute 3 + 4 = 7, push 7.*
, pop 2 and 7, compute 7 * 2 = 14, push 14./
, pop 7 and 14, compute 14 / 7 = 2, push 2.The function should return 2
.
Note: April 25 is ANZAC Day, a day of remembrance. As you write your code, take a moment to reflect on the importance of remembering history while building for the future.