From 3c90ce514e09e4bd9231020d85c3ccb0d0285ea8 Mon Sep 17 00:00:00 2001 From: tejaswipitchuka <67813989+tejaswipitchuka@users.noreply.github.com> Date: Wed, 20 Oct 2021 21:59:49 +0530 Subject: [PATCH] Add files via upload --- python/postfixexpression_evaluation.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 python/postfixexpression_evaluation.py diff --git a/python/postfixexpression_evaluation.py b/python/postfixexpression_evaluation.py new file mode 100644 index 0000000..5f209cc --- /dev/null +++ b/python/postfixexpression_evaluation.py @@ -0,0 +1,34 @@ +''' +Evaluate the value of an arithmetic expression in Reverse Polish Notation. +Valid operators are +, -, *, /.Each operand may be an integer or another expression. + +''' + +class Solution: + def evaluateRPN(self, A): + stack=[] + top=-1 + for i in A: + if(i!='+' and i!='-' and i!='*' and i!='/'): + stack.append(i) + top+=1 + continue + else: + y=int(stack.pop()) + top-=1 + x=int(stack.pop()) + top-=1 + if(i=='+'): + stack.append(x+y) + top+=1 + elif(i=='-'): + stack.append(x-y) + top+=1 + elif(i=='*'): + stack.append(x*y) + top+=1 + elif(i=='/'): + stack.append(x//y) + top+=1 + return stack[top] + \ No newline at end of file