title=Reverse Polish
Reverse Polish notation (otherwise known as post-fix, RPN for short) is a way of representing mathematical expressions. The notation is used because the format that the expression is in is easier for machines to interpret rather than the notation we are used to, infix notation, where the operator is in between the numbers. The equation can be complex or simple. RPN doesn't require brackets as the equations are laid out in such a format that they aren't required for machines to understand.
The name RPN is named after Jan Łukasiewicz, a Polish logician who invented Polish notation (prefix notation) in 1924.
Contents
Reverse Polish Notation[edit]
Reverse Polish notation should be ordered like this:
<FirstNumber> <SecondNumber> <Operation>
Rather than the normal convention (infix) of:
<FirstNumber> <Operation> <SecondNumber>
Examples[edit]
Examples of equivalent equations[edit]
PostFix | Infix | Answer |
---|---|---|
3 4 + | 3 + 4 | 7 |
3 5 6 + * | (5 + 6) * 3 | 33 |
2 4 / 5 6 - * | (2/4)*(5-6) | -0.5 |
2 3 ↑ | 2³ | 8 |
Implementation as a Stack[edit]
YouTube Video Example[edit]
A video example can be seen here from Computerphile on Youtube: https://www.youtube.com/watch?v=7ha78yWRDlE ^{[1]}
Practice Questions[edit]
Exercise: Infix to RPN Conversion
Convert x + y into RPN. Answer : x y + Convert (x + y)*z into RPN. Answer : x y + z * Convert 3 / (5 + y * x) into RPN. Answer : 3 5 y x * + / |
Exercise: RPN to Infix Conversion
Convert x y * into infix. Answer : x * y Convert 5 y + z x 3 - * / into infix. Answer : (5 + y) / (z * (x - 3)) |
References[edit]
- ↑ Video link to Computerphile a Youtube channel: https://www.youtube.com/channel/UC9-y-6csu5WGm29I7JiwpnA