Problem:


Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.

Write a program which reads an expression in the Reverse Polish notation and prints the computational result.

An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.

Input

An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.

You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106

Output

Print the computational result in a line.

Constraints

2 ≤ the number of operands in the expression ≤ 100

1 ≤ the number of operators in the expression ≤ 99

-1 &times; 109 ≤ values in the stack ≤ 109

Sample Input 1

1 2 +

Sample Output 1

3

Sample Input 2

1 2 + 3 4 - *

Sample Output 2

-3

Notes

Template in C



Sample input:
1 2 +


Sample output:
3

Submission status: Accepted

Solution code:
def main():
    A = input().split()
    length = len(A)
    stack = []
    for i in range(length):
        if A[i] == "+":
            stack.insert( 0, stack[1] + stack[0] )
            stack.pop(1)
            stack.pop(1)
        elif A[i] == "-":
            stack.insert( 0, stack[1] - stack[0] )
            stack.pop(1)
            stack.pop(1)
        elif A[i] == "*":
            stack.insert( 0, stack[1] * stack[0] )
            stack.pop(1)
            stack.pop(1)
        else :
            stack.insert( 0, int(A[i]) )
    print( stack[0] )

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
st = []
for c in input().split():
    if c in "+-*":
        v2 = st.pop()
        v1 = st.pop()
        st.append(str(eval(v1 + c + v2)))
    else:
        st.append(c)
print(st[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

top = []


def push(x):
    top.append(x)


def pop(x, cnt):
    a = int(top[cnt])
    top.pop(cnt)
    b = int(top[cnt - 1])
    top.pop(cnt - 1)

    if x == '+':
        top.append(b + a)
    elif x == '-':
        top.append(b - a)
    elif x == '*':
        top.append(b * a)


n = list(input().split())
cnt = -1

for i in n:
    if i.isdigit():
        push(i)
        cnt += 1
    else:
        pop(i, cnt)
        cnt -= 1

print(top[0])

Submission status: Accepted

Solution code:
exp = input().split()

stack = []

for e in exp:
    if e.isdecimal():
        stack.append(int(e))
    else:
        b = stack.pop()
        a = stack.pop()
        if   e == '+': a += b
        elif e == '-': a -= b
        elif e == '*': a *= b
        else: raise
        stack.append(a)

assert len(stack) == 1

print(stack[0])

Submission status: Accepted

Solution code:
eq = raw_input().split()

res = []
for e in eq:
	if e.isdigit():
		res.append(int(e))
		
	else:
		o2 = res.pop()
		o1 = res.pop()
		exec("res.append(o1 %s o2)" % e)

print res[0]

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin
exppression = stdin.readline().split()
stack = []
while exppression:
    token = exppression.pop(0)
    if token.isdigit():
        stack.append(int(token))
    elif token == '+':
        n = stack.pop()
        stack[-1] += n
    elif token == '-':
        n = stack.pop()
        stack[-1] -= n
    else:
        n = stack.pop()
        stack[-1] *= n
print(stack[-1])

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
l=map(str, raw_input().split())
k=[]
for i in l:
    if i=='-' or i=='+' or i=='*':
        t=k.pop()
        s=k.pop()
        u=str(s)+i+str(t)
        k.append(eval(u))
    else:
        k.append(i)
print k[0]

Submission status: Accepted

Solution code:
inp = map(str, raw_input().split())
s = []
for i in inp:
    if   i == "+": s.append( s.pop(-2) + s.pop(-1) )
    elif i == "-": s.append( s.pop(-2) - s.pop(-1) )
    elif i == "*": s.append( s.pop(-2) * s.pop(-1) )
    else: s.append(int(i))
print s[0]

Submission status: Accepted

Solution code:
def operate(operand0, operand1, operator):
    if operator == "+":
        return str(int(operand0) + int(operand1))
    elif operator == "*":
        return str(int(operand0) * int(operand1))
    elif operator == "-":
        return str(int(operand0) - int(operand1))

if __name__ == "__main__":
    import sys
    expr = sys.stdin.readline().strip().split()
    stack = []
    for t in expr:
        if t.isdigit():
            stack.append(t)
        else:
            a = stack.pop()
            b = stack.pop()
            stack.append(operate(b, a, t))
    print stack.pop()

Submission status: Accepted

Solution code:
def f(exp):
    from operator import add, mul
    ops = {"+": add, "-": lambda a, b:b - a, "*": mul}
    stack = []
    def g(s):
        if s in ops:
            stack.append(ops[s](stack.pop(), stack.pop()))
        else:
            stack.append(int(s))
    map(g, exp.split())
    return stack[-1]
print f(raw_input())

Submission status: Accepted

Solution code:
ops = {"+": lambda a, b: b + a,
       "-": lambda a, b: b - a,
       "*": lambda a, b: b * a}
def f(stack, s):
    if s in ops:
        stack.append(ops[s](stack.pop(), stack.pop()))
    else:
        stack.append(int(s))
    return stack

print reduce(f, raw_input().split(), [])[-1]

Submission status: Accepted

Solution code:
print reduce(lambda stack, s: stack[:-2] + [eval("{} {} {}".format(stack[-2], s, stack[-1]))] if s in "+-*" else stack + [int(s)], raw_input().split(), [])[-1]

Submission status: Accepted

Solution code:
f = input().strip().split()
q = []
for e in f:
    if e == '+':
        q.append(q.pop() + q.pop())
    elif e == '-':
        q.append(-q.pop() + q.pop())
    elif e == '*':
        q.append(q.pop() * q.pop())
    else:
        q.append(int(e))
print(q[0])

Submission status: Accepted

Solution code:
A = list(input().split())
#print(A)
stack = list()
ans = 0
x = 0

for i in range(len(A)):
    if (A[i] == "+"):
        l = len(stack)
        x = stack[l-2] + stack[l-1]
        del stack[-1]
        del stack[-1]
        stack.append(x)

    elif (A[i] == "-"):
        l = len(stack)
        x = stack[l-2] - stack[l-1]
        del stack[-1]
        del stack[-1]
        stack.append(x)

    elif (A[i] == "*"):
        l = len(stack)
        x = stack[l-2] * stack[l-1]
        del stack[-1]
        del stack[-1]
        stack.append(x)

    elif (A[i] == "/"):
        l = len(stack)
        x = stack[l-2] / stack[l-1]
        del stack[-1]
        del stack[-1]
        stack.append(x)

    else :
        stack.append(int(A[i]))
print(stack[0])

Submission status: Accepted

Solution code:
def main():
    args = input().split()

    list = []
    for x in args:
        if x == "+":
            b = list.pop()
            a = list.pop()
            list.append(a + b)
        elif x == "-":
            b = list.pop()
            a = list.pop()
            list.append(a - b)
        elif x == "*":
            b = list.pop()
            a = list.pop()
            list.append(a * b)
        else:
            list.append(int(x))

    print(list[0])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
input_string = list(input().split())

stack = []
for i in range(len(input_string)):
    a = input_string[i]
    if a in ["+", "-", "*"]:
        num1 = stack.pop()
        num2 = stack.pop()
        if a == "+":
            stack.append(num2 + num1)
        elif a == "-":
            stack.append(num2 - num1)
        else:
            stack.append(num2 * num1)
    else:
        stack.append(int(a))
print(stack[0])

Submission status: Accepted

Solution code:
A = input().split()

S =[0]
def push(x):
    S.append(x)

def pop(S):
    top_id = len(S) - 1
    top = S.pop(top_id)
    return top

for i in range(len(A)):

    if A[i] == '+':
        push(pop(S) + pop(S))
        continue
    
    if A[i] == '-':
        push(-pop(S)+pop(S))
        continue
    
    if A[i] == '*':
        push(pop(S)*pop(S))
        continue
    
    else:
        A[i] = int(A[i])
        push(A[i])

print(pop(S))

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

A = LS()

acum = 0
stack = []
for a in A:
    if a == '+':
        acum = stack.pop()
        acum += stack.pop()
        stack.append(acum)
    elif a == '-':
        acum = stack.pop()
        acum = stack.pop() - acum
        stack.append(acum)
    elif a == '*':
        acum = stack.pop()
        acum *= stack.pop()
        stack.append(acum)
    else:
        stack.append(int(a))
print(acum)

Submission status: Accepted

Solution code:
#stackのクラスを実装
class stack():
    def __init__(self):
        self.S = []
        self.maxlen= 1000000
        
    def push(self, x):
        if len(self.S) == self.maxlen:
            print('オーバーフロー')
        else:
            self.S.append(x)
        
    def pop(self):
        if len(self.S) == 0:
            print('アンダーフロー')
            return False
        else:
            return self.S.pop()
    
    def isEmpty(self):
        if len(self.S) == 0:
            return True
        else:
            return False
    
    def isFull(self):
        if len(self.S) == self.lenmax:
            return True
        else:
            return False
        
stk = stack()
rpn = input().split()

for i in range(len(rpn)):
    if rpn[i] == '+':
        y = stk.pop()
        x = stk.pop()
        stk.push(x+y)
    elif rpn[i] == '-':
        y = stk.pop()
        x = stk.pop()
        stk.push(x-y)
    elif rpn[i] == '*':
        y = stk.pop()
        x = stk.pop()
        stk.push(x*y)
    elif rpn[i] == '/':
        y = stk.pop()
        x = stk.pop()
        stk.push(x/y)
    else:
        stk.push(int(rpn[i]))
print(stk.pop())

Submission status: Accepted

Solution code:
'''
スタックを用いた逆ポーランド記法の計算機の実装
・文字列が数字であればスタックに積む
・文字列が演算子であれば、スタックから2個値を取り出し演算結果をスタックに積む
'''

from collections import deque


def reverse_polish_notation(lst):
    stack = deque()
    for i in lst:
        if str(i).isdecimal():
            stack.append(int(i))
        else:
            if i == "+":
                b = stack.pop()
                a = stack.pop()
                stack.append(a+b)
            if i == "-":
                b = stack.pop()
                a = stack.pop()
                stack.append(a-b)
            if i == "*":
                b = stack.pop()
                a = stack.pop()
                stack.append(a*b)
    return stack[0]


lst = list(input().split())
print(reverse_polish_notation(lst))

Submission status: Accepted

Solution code:
class Stack():
    def __init__(self):
        self.stack = list()

    def _is_empty(self):
        if len(self.stack) == 0:
            return True
        return False

    def push(self, obj):
        self.stack.append(obj)

    def pop(self):
        if self._is_empty():
            print('empty')
            return None
        return self.stack.pop(-1)

    def __len__(self):
        return len(self.stack)


def main():
    sentense = input().split(' ')
    stack = Stack()
    for chr in sentense:
        if chr in ['+', '-', '*']:
            b = stack.pop()
            a = stack.pop()
            if chr == '+':
                stack.push(a+b)
            elif chr == '-':
                stack.push(a-b)
            else:
                stack.push(a*b)
        else:
            stack.push(int(chr))
    print(stack.pop())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = [0 for _ in range(1000)]
A = list(input().split())
top = 0

def initialize():
  global top
  top = 0

def isEmpty():
  global top
  return top == 0

def isFull():
  global top
  return top > 1000

def push(x):
  if isFull():
    print('over flow')
  global top
  top += 1
  S[top] = x

def pop():
  if isEmpty():
    print('under flow')
  global top
  top -= 1
  return S[top+1]

for i in range(len(A)):
  if A[i] == '*':
    b = pop()
    a = pop()
    result = a * b
    push(result)
  elif A[i] == '+':
    b = pop()
    a = pop()
    result = a + b
    push(result)
  elif A[i] == '-':
    b = pop()
    a = pop()
    result = a - b
    push(result)
  else:
    push(int(A[i]))

print(S[1])

Submission status: Accepted

Solution code:
import re
F = list(input().split())
S = []
while F != []:
    if re.match(r'[0-9]', F[0]):
        S.append(F.pop(0))
    elif F[0] == '+':
        F.pop(0)
        x = int(S.pop())
        y = int(S.pop())
        S.append(x+y)
    elif F[0] == '-':
        F.pop(0)
        x = int(S.pop())
        y = int(S.pop())
        S.append(y-x)
    elif F[0] == '*':
        F.pop(0)
        x = int(S.pop())
        y = int(S.pop())
        S.append(x*y)

print(S[0])

Submission status: Accepted

Solution code:
A = list(input().split())

num = []
for i in range(len(A)):
    if A[i]=='+':
        n = num[len(num)-2] + num[len(num)-1]
        del(num[len(num)-2:len(num)])
        num.append(n)
    elif A[i]=='-':
        n = num[len(num)-2] - num[len(num)-1]
        del(num[len(num)-2:len(num)])
        num.append(n)
    elif A[i]=='*':
        n = num[len(num)-2] * num[len(num)-1]
        del(num[len(num)-2:len(num)])
        num.append(n)
    else:
        num.append(int(A[i]))

print(num[0])

Submission status: Accepted

Solution code:
arr = list(input().split())
stack = []

for i in arr:
    if i == "+":
        stack.append(stack.pop()+stack.pop())
    elif i == "-":
        stack.append(-(stack.pop()-stack.pop()))
    elif i =="*":
        stack.append(stack.pop()*stack.pop())
    else:
        stack.append(int(i))
print(stack[-1])

Submission status: Accepted

Solution code:
Input = input().split()

Operator = ["+","-","*"]
stack = []

for i in Input:
    if i in Operator:
        y = stack.pop()
        x = stack.pop()
        tmp = eval(x+i+y)
        stack.append(str(tmp))

    else:
        stack.append(i)

print(stack.pop())

Submission status: Accepted

Solution code:
stk=[]
data=(input().split())
for i in data:
    if i =='+':
        a=stk.pop()
        b=stk.pop()
        stk.append(a+b)
    elif i =='-':
        b=stk.pop()
        a=stk.pop()
        stk.append(a-b)
    elif i =='*':
        a=stk.pop()
        b=stk.pop()
        stk.append(a*b)
    else:
        stk.append(int(i))
print(stk.pop())

Submission status: Accepted

Solution code:
def main():
    formula = input().split()
    stack = []
    for f in formula:
        if f == '+':
            stack.append(stack.pop() + stack.pop())
        elif f == '-':
            a = stack.pop()
            b = stack.pop()
            stack.append(b - a)
        elif f == '*':
            stack.append(stack.pop() * stack.pop())
        else:
            stack.append(int(f))
    print(stack.pop())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
A = input().split()
stack = []

for i in range(len(A)):
  if A[i] == "+":
    b = stack.pop()
    a = stack.pop()
    stack.append(a+b)
  elif A[i] == "-":
    b = stack.pop()
    a = stack.pop()
    stack.append(a-b)
  elif A[i] == "*":
    b = stack.pop()
    a = stack.pop()
    stack.append(a*b)
  else:
    stack.append(int(A[i]))

print(stack.pop())

Submission status: Accepted

Solution code:
import sys

class stacker():
    def __init__(self, max):
        self.top = 0
        self.max = max
        self.vlist = list(range(max))
        
    def push(self, x):
        self.top += 1
        self.vlist[self.top] = x
        
    def pop(self):
        self.top -= 1
        return self.vlist[self.top+1]
        
    def isFull(self):
        return self.top == self.max
    
    def isEmpty(self):
        return  self.top == 0
    
def main():

    expr = list(input().split())
    stack = stacker(200)
    for i in expr:
        if i == '+':
            a = stack.pop()
            b = stack.pop()
            stack.push(a+b)
        elif i == '-':
            a = stack.pop()
            b = stack.pop()
            stack.push(b-a)
        elif i == '*':
            a = stack.pop()
            b = stack.pop()
            stack.push(a*b)
        else:
            stack.push(int(i))

    print(stack.vlist[stack.top])
    
            
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
ops = {"+":lambda x,y: y+x, "-":lambda x,y: y-x, "*":lambda x,y: y*x}
stack = []
for a in input().split():
    stack.append(ops[a](stack.pop(), stack.pop()) if a in ops else int(a))
print(stack.pop())

Submission status: Accepted

Solution code:
a = list(input().split())
b = []
for i in a:
    if i in '+-*':
        n1 = b.pop()
        n2 = b.pop()
        b.append(str(eval(n2+i+n1)))
    else:
        b.append(i)
    
print(b.pop())

Submission status: Accepted

Solution code:

a = list(input().split())
stc = []

for i in range(len(a)):
    if a[i]=="+":
        stc.append(stc.pop() + stc.pop())
    elif a[i]=="-":
        stc.append(-(stc.pop() - stc.pop()))
    elif a[i]=="*":
        stc.append(stc.pop() * stc.pop())
    else:
        stc.append(int(a[i]))
        
print(stc[-1])

Submission status: Accepted

Solution code:
class stack:
    def __init__(self,arg_size):
        self.size = arg_size
        self.stack = [None] * self.size
        self.head = 0
        self.tail = 0

    def isEmpty(self):
        return self.tail == self.head
    
    def isFull(self):
        return len(self.stack) == self.tail

    def push(self, arg_object):
        self.stack[self.tail] = arg_object
        self.tail += 1

    def pop(self):
        self.tail -= 1
        return self.stack[self.tail]

    def top(self):
        return self.stack[self.head]

num = 101
s = stack(num)

po = list(input().split(' '))

for i in po:
    if i not in '+-*':
        s.push(int(i))
    
    else:
        a = s.pop()
        b = s.pop()
        if i == '+':
            s.push(b+a)
        elif i == '*':
            s.push(b*a)
        else:
            s.push(b-a)

print(s.top())

Submission status: Accepted

Solution code:
a=[]
for i in input().split():
    if i in "+-*":
        x,y=a.pop(),a.pop()
        a.append(str(eval(y+i+x)))
    else:
        a.append(i)
print(a[0])

Submission status: Accepted

Solution code:
import sys
P = list(map(str, input().split()))
MAX = len(P)
S = [0] * MAX

def initialize():
    top = 0
    return top

def isEmpty():
    return top == 0

def isFull():
    return top >= MAX - 1

def push(x, top):
    if isFull():
        print('Error')
        sys.exit()
    top += 1
    S[top] = x
    return top

def pop(top):
    if isEmpty():
        print('Error')
        sys.exit()
    top -= 1
    return top, S[top + 1]

top = initialize()
for i in range(len(P)):
    if P[i].isdecimal():
        top = push(int(P[i]), top)
    elif P[i] == '+':
        top, n1 = pop(top)
        top, n2 = pop(top)
        top = push(n2 + n1, top)
    elif P[i] == '-':
        top, n1 = pop(top)
        top, n2 = pop(top)
        top = push(n2 - n1, top)
    elif P[i] == '*':
        top, n1 = pop(top)
        top, n2 = pop(top)
        top = push(n2 * n1, top)
print(S[top])

Submission status: Accepted

Solution code:
class Stack:

  def __init__(self):
    self.stack = []

  def push(self, value):
    self.stack.append(value)

  def pop(self):
    if len(self.stack) == 0:
      raise Exception("アンダーフロー") 
    return self.stack.pop()

s = Stack()
input_str = list(input().split())

for i in input_str:
  if i == "+":
    a = s.pop()
    b = s.pop()
    s.push(a+b)

  elif i == "-":
    b = s.pop()
    a = s.pop()
    s.push(a-b)

  elif i == "*":
    a = s.pop()
    b = s.pop()
    s.push(a*b)

  else:
    s.push(int(i))

print(s.pop())

Submission status: Accepted

Solution code:
s=list(map(str,input().split()))
c=[]
for i in range(len(s)):
    if s[i]=="+":
        a=c.pop(-1)
        b=c.pop(-1)
        c.append(b+a)
    elif s[i]=="-":
        a=c.pop(-1)
        b=c.pop(-1)
        c.append(b-a)
    elif s[i]=="*":
        a=c.pop(-1)
        b=c.pop(-1)
        c.append(b*a)
    else:
        c.append(int(s[i]))
print(c[0])

Submission status: Accepted

Solution code:
stack = []

for number in input().split():  #入力をリストにかえ、forで前から一つずつ読み込んでいる
    if number not in '+-*':
        stack.append(number)
    else:
        operator = number
        number1 = stack.pop()
        number2 = stack.pop()
        calculated = eval(number2 + operator + number1) #文字列の式を計算している。戻り値が数字になっている
        stack.append(str(calculated))
        #print("{} {} {} = {}".format(number2,operator,number1,calculated))
print(stack[0])

Submission status: Accepted

Solution code:
s = list(input().split())
stack = []

for i in range(len(s)):
    if s[i] == '+':
        stack.append(stack.pop() + stack.pop())
    elif s[i] == '-':
        stack.append(-(stack.pop() - stack.pop()))
    elif s[i] == '*':
        stack.append(stack.pop() * stack.pop())
    else:
        stack.append(int(s[i]))

print(stack[-1])

Submission status: Accepted

Solution code:
class Stack:
    def __init__(self,arg_size):
        self.size = arg_size
        self.stack = [None]*arg_size
        self.head = 0
        self.tail = 0
    def is_empty(self):
        return self.tail == self.head
    def isFull(self):
        return self.tail == len(self.stack) - 1
    def push(self,arg_object):
        self.stack[self.tail] = arg_object
        self.tail += 1
    def pop(self):
        self.tail -= 1
        return self.stack[self.tail]
    def top(self):
        return self.stack[self.head]

size = 105
NUM = Stack(size)

input_str = list(map(str,input().split()))

for i in range(len(input_str)):
    T = input_str[i]
    if T in ['+','-','*']:
        num2 = NUM.pop()
        num1 = NUM.pop()
        num = 0
        if T == '+':
            num = num1 + num2
        elif T == '-':
            num = num1 - num2
        elif T == '*':
            num = num1 * num2
        NUM.push(num)
    else:
        NUM.push(int(T))

print(NUM.pop())

Submission status: Accepted

Solution code:
stack =[]
line = input().split(' ')
for i in line:
    if i == '+':
        num1 = stack.pop()
        num2 = stack.pop() 
        stack.append(num1 + num2)
    elif i == '-':
        num1 = stack.pop()
        num2 = stack.pop() 
        stack.append(num2 - num1)
    elif i == '*':
        num1 = stack.pop()
        num2 = stack.pop() 
        stack.append(num2 * num1)
    else:
        stack.append(int(i))
print(stack.pop())

Submission status: Accepted

Solution code:
ops = list(input().split())
stk = []
for i in ops:
    if i == '+':
        y = stk.pop()
        x = stk.pop()
        stk.append(int(x) + int(y))
    elif i == '-':
        y = stk.pop()
        x = stk.pop()
        stk.append(int(x) - int(y))
    elif i == '*':
        y = stk.pop()
        x = stk.pop()
        stk.append(int(x) * int(y))
    else:
        stk.append(i)
print(stk[0])

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
li = list(input().split())
for i in li:
    if i in "+-*":
        b = d.pop()
        a = d.pop()
        d.append(str(eval(a + i + b)))
    else:
        d.append(i)

print(str(d[0]))

Submission status: Accepted

Solution code:
A = []
for p in input().split():
    if p in "+-*":
        A.append(str(eval(A.pop(-2) + p + A.pop())))
    else:
        A.append(p)
print(*A)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque
import copy

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    S = sl()
    stack = []
    operation = {
        '+': lambda a, b: b + a,
        '-': lambda a, b: b - a,
        '*': lambda a, b: b * a,
    }

    for s in S:
        if s.isdecimal():
            stack.append(s)
        else:
            stack.append(operation[s](int(stack.pop()), int(stack.pop())))
    print(*stack)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
num = []
for i in input().split():
    if i == "+":
        a = num.pop()
        b = num.pop()
        num.append(a+b)
    elif i == "-":
        a = num.pop()
        b = num.pop()
        num.append(b-a)
    elif i == "*":
        a = num.pop()
        b = num.pop()
        num.append(a*b)
    else:
        num.append(int(i))
print(num[0])

Submission status: Accepted

Solution code:
from collections import deque
d = deque()
S = input().split()
for s in S:
    if s == "+":
        a = d.pop()
        b = d.pop()
        d.append(a+b)
    elif s == "-":
        a = d.pop()
        b = d.pop()
        d.append(b-a)
    elif s == "*":
        a = d.pop()
        b = d.pop()
        d.append(a*b)
    else:
        d.append(int(s))

print(d[0])

Submission status: Accepted

Solution code:
import re

tokens = list(input().split())
stack = []

for token in tokens:
   if  re.match('\d', token):
       stack.append(int(token))
   else:
        rval = stack.pop(-1)
        lval = stack.pop(-1)
        if token == '+':
            result = lval + rval
        elif token == '-':
            result = lval - rval
        elif token == '*':
            result = lval * rval
        stack.append(result)

print(stack.pop(-1))

Submission status: Accepted

Solution code:
# coding: utf-8

class Stack:
    def __init__(self):
        self.data = []

    def push(self, d):
        self.data.append(d)

    def pop(self):
        if not self.data:
            return None
        else:
            return self.data.pop()

    def print(self):
        print(self.data)

s = Stack()

input_data = input()
#input_data = "1 2 +"
#input_data = "1 2 + 3 4 - *"

# push data in stack
for d in input_data.split(' '):
    if d == "+":
        s.push(s.pop() + s.pop())
    elif d == "-":
        tmp1 = s.pop()
        tmp2 = s.pop()
        s.push(tmp2 - tmp1)
    elif d == "*":
        tmp1 = s.pop()
        tmp2 = s.pop()
        s.push(tmp2 * tmp1)
    else:
        s.push(int(d))

print(s.pop())

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-

def main():
    formula=[]
    formula = list(input().split())
    stack=[]
    value1=0
    value2=0
    for value in formula:
        if "+" in value or "-" in value or "*" in value:
            value2=stack.pop()
            value1=stack.pop()
            stack.append(str(eval(value1 + value + value2)))
        else:
            stack.append(value)
    print(stack[0])

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


class Node:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.parent = None


class Stack:
    def __init__(self):
        self.head = Node(None)
        self.top = self.head

    def push(self, node: Node):
        if self.top == self.head:
            self.head.next = node
            node.parent = self.head
            self.top = node
        else:
            self.top.next = node
            node.parent = self.top
            self.top = node

    def pop(self):
        if self.top == self.head:
            return None
        else:
            _tmp = self.top
            self.top = self.top.parent
            self.top.next = None
            return _tmp


ops = input().strip().split()

stack = Stack()

for op in ops:
    if op == "+":
        _a = stack.pop().val
        _b = stack.pop().val
        stack.push(Node(_a + _b))
    elif op == "-":
        _a = stack.pop().val
        _b = stack.pop().val
        stack.push(Node(_b - _a))
    elif op == "*":
        _a = stack.pop().val
        _b = stack.pop().val
        stack.push(Node(_a * _b))
    else:
        stack.push(Node(int(op)))

print(stack.pop().val)

Submission status: Accepted

Solution code:
import math

stack=[]
s=raw_input().split()

for i in s:
  if i=='+' or i=='-' or i=='*' or i=='/':
    x=stack.pop()
    y=stack.pop()
    stack.append(str(eval(y+i+x)))

  else:
    stack.append(i)

print stack[0]

Submission status: Accepted

Solution code:
codes = input().split()

stack = []
for index in range(len(codes)):
    num_or_operator = codes[index]
    if num_or_operator.isdigit():
        stack.append(int(num_or_operator))
    else:
        rhs = stack.pop()
        lhs = stack.pop()
        if num_or_operator == "+":
            stack.append(lhs + rhs)
        elif num_or_operator == "-":
            stack.append(lhs - rhs)
        else:
            stack.append(lhs * rhs)

print(stack.pop())

Submission status: Accepted

Solution code:
exp = input().split()
op = '+ - *'.split()
stack = []

for e in exp:
    if e == '+':
        ope1 = stack.pop()
        ope2 = stack.pop()
        stack.append(ope2 + ope1)
    elif e == '-':
        ope1 = stack.pop()
        ope2 = stack.pop()
        stack.append(ope2 - ope1)
    elif e == '*':
        ope1 = stack.pop()
        ope2 = stack.pop()
        stack.append(ope2 * ope1)
    else:
        stack.append(int(e))

print(stack[0])

Submission status: Accepted

Solution code:
stack = []
lst = input().split()
for i in range(len(lst)) :
    if lst[i] in ['+', '-', '*', '/'] :
        b = stack.pop(-1)
        a = stack.pop(-1)
        if lst[i] == '+' :
            stack.append(a + b)
        elif lst[i] == '-' :
            stack.append(a - b)
        elif lst[i] == '*' :
            stack.append(a * b)
        else :
            stack.append(a // b)
    else :
        stack.append(int(lst[i]))
        
print(stack[0])