In this exercise, you'll implement a function that evaluates a mathematical expression written in postfix notation (also known as Reverse Polish Notation). In a postfix expression, every operator follows all of its operands. For example, the postfix expression 3 4 + 2 *
is evaluated as:
Your task is to parse the given string (where tokens are separated by spaces) and return the evaluated result as a number.
Input: A string containing a valid postfix expression consisting of non-negative integers and the operators +
, -
, *
, /
.
Output: A number representing the result of the evaluated expression.
Note: Division should be treated as integer division if your language does not automatically use floating-point arithmetic where appropriate.
Fun Fact: On March 11th, we celebrate creativity and innovation. Use your stack skills to build an innovative solution!
For the input: "5 1 2 + 4 * + 3 -"
, the function should return 14
.
Below you will find starter code snippets in multiple languages to help you begin your solution.