From 9b1243b96598efda4f4410454f0d35b4f4f88bb7 Mon Sep 17 00:00:00 2001 From: Gael Yimen Yimga <7570881+yimengael@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:24:11 -0700 Subject: [PATCH 1/2] Evaluate an RPN expression Evaluate an RPN expression --- .../Stacks/Evaluate-RPN-Expression.md | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md diff --git a/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md b/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md new file mode 100644 index 0000000..55b75a2 --- /dev/null +++ b/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md @@ -0,0 +1,70 @@ +# Evaluate an RPN (Reverse Polished Notation) expression. +## Problem: +A string is said to be an arithmetical expression in Reverse Polish notation (RPN), if: +- It is a single digit or a sequence of digits, prefixed with an option -, e.g., "6", "123", "-42". +- It is of the form“A,B,o" where A and B are RPN expressions and o is one of +,-,*,/ +For example, the following strings satisfy these rules: "1729", "3,4,+,2,X,1,+", "1,1,+,-2,x","-641,6,/,28,/". + +Write a program that takes an arithmetical expression in RPN and returns the number that the expression evaluates to. + +## Thinking process +The first thing that comes to my mind when we are talking about evaluating an expression, is the use of the stack to do so. Given the fact that the expression is an RPN (Reverse Polish Notation), we can straightly evaluate. + +Let's suppose we have an expression such as the following s = "3,4,+,2,*,1,+". The first thing to do is to split the string based on the comma delimiter. We will have a set of tokens to traverse. + +- We create a stack to store all the operands. +- We traverse the list of the tokens. +- If the current token is a number, then we push that token onto the stack. +If the current token is an operator, we pop the two operands on top of the stack and apply the operator and push the result back onto the stack +and continue to traverse the list of tokens. +- When we are done traversing the list of tokens, the last element in the stack is the result the expression evaluates to. + +We assume that the expression will be given in the form of "3,4,+,2,*,1,+" with comma separated. + +The time complexity is O(n) which is the number of characters in the given expression, and the space complexity is O(2), which is constant. + + +## Code +``` +public int evaluateRPNExpresion(String expr) { + Stack stack = new Stack(); + String[] tokens = expr.split(","); + for (String token : tokens) { + if (isOperator(token)) { + char c = token.trim().charAt(0); + int val = 0; + int val1 = stack.pop(); + int val2 = stack.pop(); + switch(c) { + case '+': + val = val1 + val2; + break; + case '-': + val = val1 - val2; + break; + case '*': + val = val1 * val2; + break; + case '/': + if (val == 0) { + throw new IllegaStateException("Division By zero"); + } + val = val1 / val2; + break; + } + stack.push(val); + } else { + stack.push(Integer.parseInt(token.trim())); + } + } + return stack.peek(); +} + +public boolean isOperator(String s) { + Set opSet = new HashSet<>(Arrays.asList('+', '-', '*', '/')); + if (s == null || s.length() = 0) { + return false; + } + return s.length() == 1 && opSet.contains(s.charAt(0)); +} +``` From 5d78fc3edb8c67b511202e5cbd818bc818abbe3f Mon Sep 17 00:00:00 2001 From: Gael Yimen Yimga <7570881+yimengael@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:29:44 -0700 Subject: [PATCH 2/2] Update Evaluate-RPN-Expression.md --- .../Stacks/Evaluate-RPN-Expression.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md b/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md index 55b75a2..cf82b84 100644 --- a/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md +++ b/Data Structures And Algorithms/Stacks/Evaluate-RPN-Expression.md @@ -51,6 +51,8 @@ public int evaluateRPNExpresion(String expr) { } val = val1 / val2; break; + default: + throw new IllegalArgumentException("Malformed RPN expression."); } stack.push(val); } else {