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:
class MyStack(object):
    """stack class

    Attributes:
        stack: stack
    """

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

    def pop(self):
        """pop method

        Returns:
            popped object
        """
        return self.stack.pop()

    def push(self, value):
        """ push method
        Args:
            value: value to be pushed
        """
        self.stack.append(value)


sequence = raw_input().split()
operators = {"*": "*", "+": "+", "-": "-"}
stack = MyStack()
for s in sequence:
    if s in operators:
        value2 = stack.pop()
        value1 = stack.pop()
        stack.push(str(eval(value1 + operators[s] + value2)))
    else:
        stack.push(s)

print(stack.pop())

Submission status: Accepted

Solution code:
class MyStack(object):
    """stack class

    Attributes:
        stack: stack
    """

    def __init__(self):
        self.stack = [0] * 1000
        self.top = 0

    def pop(self):
        """pop method

        Returns:
            popped object
        """
        if self.top - 1 < 0:
            print("Stack Underflow")
        else:
            value = self.stack[self.top]
            self.stack[self.top] = 0
            self.top -= 1
            return value

    def push(self, value):
        """ push method
        Args:
            value: value to be pushed
        """
        if self.top + 1 >= len(self.stack):
            print("Stack Overflow")
        else:
            self.top += 1
            self.stack[self.top] = value

sequence = raw_input().split()
operators = {"*": "*", "+": "+", "-": "-"}
stack = MyStack()
for s in sequence:
    if s in operators:
        value2 = stack.pop()
        value1 = stack.pop()
        stack.push(str(eval(value1 + operators[s] + value2)))
    else:
        stack.push(s)

print(stack.pop())

Submission status: Accepted

Solution code:
#python2.7

A=raw_input().split()
B=[]
for i in A:
	if i.isdigit():
		B.append(i)
	else:
		if i=="+":
			B[-1]=str(int(B[-1])+int(B[-2]))
			del B[-2]
		elif i=="-":
			B[-1]=str(int(B[-2])-int(B[-1]))
			del B[-2]
		elif i=="*":
			B[-1]=str(int(B[-1])*int(B[-2]))
			del B[-2]
print B[0]

Submission status: Accepted

Solution code:
def do_calc(data):
    from collections import deque
    s = deque()  # ??????????????¨??????????????????
    while data:
        elem = data[0]
        data = data[1:]
        if elem == '+' or elem == '-' or elem == '*':
            a = s.pop()
            b = s.pop()
            if elem == '+':
                s.append(b + a)
            elif elem == '-':
                s.append(b - a)
            elif elem == '*':
                s.append(b * a)
        else:
            s.append(int(elem))
    return s.pop()


if __name__ == '__main__':
    # ??????????????\???
    data = [x for x in input().split(' ')]
    # data = ['1', '2', '+', '3', '4', '-', '*']

    # ???????????????
    result = do_calc(data)

    # ???????????????
    print(result)

Submission status: Accepted

Solution code:
def do_calc(data):
    from collections import deque
    s = deque()  # ??????????????¨??????????????????
    while data:
        elem = data[0]  # ?????????????????????????????????

        # ???????????§????????°?????´??????????????????pop??§????????????????¨????????????????????????????????????????
        if elem == '+' or elem == '-' or elem == '*':
            a = s.pop()
            b = s.pop()
            if elem == '+':
                s.append(b + a)
            elif elem == '-':
                s.append(b - a)
            elif elem == '*':
                s.append(b * a)
        # ??°?????§????????°????????????????????????
        else:
            s.append(int(elem))

        # ?¬??????????????????????????????????????????????????????¨??????
        data = data[1:]
    return s.pop()


if __name__ == '__main__':
    # ??????????????\???
    data = [x for x in input().split(' ')]
    # data = ['1', '2', '+', '3', '4', '-', '*']

    # ???????????????
    result = do_calc(data)

    # ???????????????
    print(result)

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

''' ???????????? '''

# pop???push??¨??????????????????????????????????????????????????¨??£??\????????????????????????
# ?¨??????????O(1)


operators = {"+": lambda x, y: x + y,
             "-": lambda x, y: x - y,
             "*": lambda x, y: x * y,
             "/": lambda x, y: x / y}


def compute(A):
    stack = []

    for i in range(len(A)):
        # ?????????
        if isinstance(A[i], str):
            op_right = stack.pop()
            op_left = stack.pop()
            # ?¨???????????????????????????????
            stack.append(operators[A[i]](op_left, op_right))

        # ???????????????
        if isinstance(A[i], int):
            stack.append(A[i])

    return stack[-1]

if __name__ == '__main__':
    A = list(input().split())
    # A = list("1 2 + 3 4 - *".split())

    for i in range(len(A)):
        if not A[i] in ['+', '-', '*', '/']:
            A[i] = int(A[i])

    print(compute(A))

Submission status: Accepted

Solution code:
class MyStack:
    def __init__(self, size):
        self.stack = [0 for i in range(size)]
        self.size = size
        self.top = 0

    def initialize(self):
        self.top = 0

    def push(self, elm):
        self.top += 1
        self.stack[self.top] = elm
        return self.stack

    def pop(self):
        ret = self.stack[self.top]
        self.stack[self.top] = 0
        self.top -= 1
        return ret

    def is_empth(self):
        if not self.is_full():
            return True
        return False

    def is_full(self):
        if self.size-1 is self.top:
            return True
        return False


def main(expr):
    stack = MyStack(1000)
    for i, elm in enumerate(expr):
        if expr[i] is '+':
            a = stack.pop()
            b = stack.pop()
            stack.push(int(a) + int(b))
        elif expr[i] is '-':
            a = stack.pop()
            b = stack.pop()
            stack.push(int(b) - int(a))
        elif expr[i] is '*':
            a = stack.pop()
            b = stack.pop()
            stack.push(int(a) * int(b))
        else:
            stack.push(elm)
    return stack.pop()


if __name__ == '__main__':
    expr = [str(s) for s in input().split()]
    print(main(expr))

Submission status: Accepted

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

stack = list()

def deal_expression(x):
    if x.isdigit():
        stack.append(int(x))
    else:
        a = stack.pop()
        b = stack.pop()
        if x == '+':
            stack.append(b + a)
        elif x == '-':
            stack.append(b - a)
        elif x == '*':
            stack.append(b * a)
        elif x == '/':
            stack.append(b / a)

for x in input().split():
    deal_expression(x)
print(stack[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ope = {"+": lambda a, b: a + b, "-": lambda a, b: b - a, "*": lambda a, b: a * b, "/": lambda a, b: a / b}
stack =[]

for i in input().split():
    if i in ope:
        stack.append(ope[i](stack.pop(),stack.pop()))
    else:
        stack.append(int(i))

print(stack[0])

Submission status: Accepted

Solution code:
operators = ['+', '-', '*', '/']
stack = list(input().split())
# stack = list(open('input.txt').readline().split())


def calc(ope):
    c = stack.pop()
    a = c if c not in operators else calc(c)
    c = stack.pop()
    b = c if c not in operators else calc(c)
    return str(eval(b+ope+a))

print(calc(stack.pop()))

Submission status: Accepted

Solution code:

def revPoland(expr):
    stack = []

    for l in expr:
        if l.isdigit():
            stack.append(int(l))
            # print(stack)
        else:
            a = stack.pop()
            b = stack.pop()
            c = eval(str(b) + l + str(a))
            stack.append(c)
            # print(stack)

    return stack.pop()

expr = input().split()

print(revPoland(expr))

Submission status: Accepted

Solution code:
operand = input().split()
stack = []
for op in operand:
    if op.isdigit():
        stack.append(op)
    else:
        val1 = stack.pop()
        val2 = stack.pop()
        stack.append(str(eval("("+val2+")" + op + "("+val1+")")))
print (stack[0])

Submission status: Accepted

Solution code:
operand = input().split()
stack = []
for op in operand:
    if op.isdigit():
        stack.append(op)
    else:
        val1 = stack.pop()
        val2 = stack.pop()
        stack.append(str(eval(val2 + op + val1)))
print (stack[0])

Submission status: Accepted

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

stack = []

for c in A:
    if c == '+':
        rhs = stack.pop()
        lhs = stack.pop()
        stack.append(lhs + rhs)
    elif c == '-':
        rhs = stack.pop()
        lhs = stack.pop()
        stack.append(lhs - rhs)
    elif c == '*':
        rhs = stack.pop()
        lhs = stack.pop()
        stack.append(lhs * rhs)
    else:
        stack.append(int(c))


print(stack.pop())

Submission status: Accepted

Solution code:
def solve(l):
    e = l.pop()
    if e == '*':
        return solve(l) * solve(l)
    elif e == '+':
        return solve(l) + solve(l)
    elif e == '-':
        return - solve(l) + solve(l)
    else:
        return int(e)

formula = input().split()
print(solve(formula))

Submission status: Accepted

Solution code:
def main():
    A = input().split()
    l = []
    operator = None
    for character in A:
        if character in ["+", "-", "*"]:
            if character == "+":
                l.append(l.pop()+l.pop())
            elif character == "-":
                l.append(-l.pop()+l.pop())
            else:
                l.append(l.pop()*l.pop())
        else:
            l.append(int(character))
    print(l[0])




if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Submission status: Accepted

Solution code:
rp = input().split()
n = len(rp)-1
i = 1
while n:
    if rp[i] in ('+', '-', '*'):
        i -= 1
        rp[i], rp[i+1] = rp[i+1], rp[i]
        rp[i-1] = str(eval(''.join(rp[i-1:i+2])))
        del rp[i:i+2]
        n -= 2
    else:
        i += 1
print(*rp)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
print((lambda s:[s.append(str(eval(s.pop(-2)+c+s.pop())) if c in "+-*" else c)or s[-1]for c in input().split()][-1])([0]))

Submission status: Accepted

Solution code:
print((lambda s:[s.append(str(eval(s.pop(-2)+c+s.pop()))if c in"+-*"else c)or s[-1]for c in input().split()][-1])([]))

Submission status: Accepted

Solution code:
x = list(input().split())
arr = []
for i in range(len(x)):
    if x[i] == '+':
        a = arr.pop()
        b = arr.pop()
        arr.append(a+b)
    elif x[i] == '-':
        b = arr.pop()
        a = arr.pop()
        arr.append(a-b)
    elif x[i] == '*':
        a = arr.pop()
        b = arr.pop()
        arr.append(a*b)
    else:
        arr.append(int(x[i]))
print(arr[-1])

Submission status: Accepted

Solution code:
def stack():
    A = list(input().split())
    lst = []
    
    def add(a, b):
        return b + a
    
    def sub(a, b):
        return b - a
        
    def mul(a, b):
        return b * a
    
    def div(a, b):
        return b / a
        
    def calc(lst, f):
        return lst.append(f(lst.pop(), lst.pop()))
    
    for i in A:
        if i == '+':
            calc(lst, add)
        elif i == '-':
            calc(lst, sub)
        elif i == '*':
            calc(lst, mul)
        elif i == '/':
            calc(lst, div)
        else: 
            lst.append(int(i))
    
    print(*lst)
    
stack()

Submission status: Accepted

Solution code:
s=list(input().split())
a=[]
for x in s:
    if x in ['+','-','*']:
        c,b=str(a.pop()),str(a.pop())
        b+=x+c
        a+=[int(eval(b))]
    else:a+=[int(x)]
print(a.pop())

Submission status: Accepted

Solution code:
data = input().split()
data.reverse()
stk = []

for i in range(len(data)):
    tmp = data.pop()
    if tmp == '+':
        tmp1 = stk.pop()
        tmp2 = stk.pop()
        stk.append(int(tmp2)+int(tmp1))
    elif tmp == '-':
        tmp1 = stk.pop()
        tmp2 = stk.pop()
        stk.append(int(tmp2)-int(tmp1))
    elif tmp == '*':
        tmp1 = stk.pop()
        tmp2 = stk.pop()
        stk.append(int(tmp2) * int(tmp1))
    else:
        stk.append(tmp)

print(stk.pop())

Submission status: Accepted

Solution code:
# stack
class stack(object):
    def __init__(self):
        self.container = [] # Empty Stack
        self.topIndex = -1
    def push(self, elem):
        self.container.append(elem)
        self.topIndex += 1
    def pop(self):
        if self.topIndex > -1:
            elem = self.container[self.topIndex]
            del self.container[self.topIndex]
            self.topIndex -= 1
            return elem
        else:
            return None

def calc(ins):
    stk = stack()
    try:
        for sin in ins:
            if sin == '+':
                stk.push(stk.pop() + stk.pop())
            elif sin == '-':
                a, b = stk.pop(), stk.pop()
                stk.push(b - a)
            elif sin == '*':
                stk.push(stk.pop() * stk.pop())
            elif sin == '/':
                a, b = stk.pop(), stk.pop()
                stk.push(b / a)
            else:
                stk.push(int(sin))
        return stk.pop()
    except:
        print 'Bad Expr'

print calc(raw_input().split(' '))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc(formura):
    if formura[-1] == '+':
        return int(formura[-3]) + int(formura[-2])
    elif formura[-1] == '-':
        return int(formura[-3]) - int(formura[-2])
    elif formura[-1] == '*':
        return int(formura[-3]) * int(formura[-2])
    elif formura[len(formura)-1] == '/':
        return int(formura[-3]) / int(formura[-2])


formura = [i for i in input().split()]
stack = []

for i in range(len(formura)):
    if formura[i] == '+' or formura[i] == '-' or formura[i] == '*' or formura[i] == '-':
        stack.append(formura[i])
        result = calc(stack)
        for j in range(3):
            stack.pop(-1)
        stack.append(result)
    else:
        stack.append(formura[i])

print(stack[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:

o = list()
top = -1
for s in input().split():
    if s.isdigit():
        o.append(int(s))
        top += 1
    else:
        if s is "+":
            n = o[top]+o[top-1]
            o.pop(top)
            top-=1
            o.pop(top)
            top-=1
            o.append(n)
            top+=1
        if s is "-":
            n = o[top-1] - o[top]
            o.pop(top)
            top -= 1
            o.pop(top)
            top -= 1
            o.append(n)
            top+=1
        if s is "*":
            n = o[top]*o[top-1]
            o.pop(top)
            top -= 1
            o.pop(top)
            top -= 1
            o.append(n)
            top+=1


print(o[0])

Submission status: Accepted

Solution code:
l = [i for i in input().split()]
s = []
for i in range(len(l)):
    if l[i] == "+":
        a = s.pop()
        b = s.pop()
        c = b + a
        s.append(c)
    elif l[i] == "-":
        a = s.pop()
        b = s.pop()
        c = b - a
        s.append(c)
    elif l[i] == "*":
        a = s.pop()
        b = s.pop()
        c = b * a
        s.append(c)
    else:
        s.append(int(l[i]))

print(s[0])

Submission status: Accepted

Solution code:
data = input().split()
stack = []
for i in data:
    try:
        stack.append(int(i))
    except:
        b = stack.pop()
        a = stack.pop()
        if i == "+":
            stack.append(a+b)
        if i == "-":
            stack.append(a-b)
        if i == "*":
            stack.append(a*b)
print(stack[0])

Submission status: Accepted

Solution code:
from collections import deque as D
line, stack = D(input().split()), D()
while line:
    hoge = line.popleft()
    if hoge.isdigit():
        stack.append(hoge)
    else:
        a, b = int(stack.pop()), int(stack.pop())
        if hoge == "*":
            stack.append(a*b)
        elif hoge == "+":
            stack.append(a+b)
        elif hoge == "-":
            stack.append(b-a)
        else:
            stack.append(b//a)
print(stack.popleft())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A = input().split()
G = []
j = 0
G.append(int(A[0]))

for i in range(1, len(A)):
	if A[i] == "+":
		G[j-1] += G[j]
		G.pop()
		j -= 1
	elif A[i] == "-":
		G[j-1] -= G[j]
		G.pop()
		j -= 1
	elif A[i] == "*":
		G[j-1] *= G[j]
		G.pop()
		j -= 1
	else:
		j += 1
		G.append(int(A[i]))

print(G[0])

Submission status: Accepted

Solution code:
calc = input().split(" ")

nums = []
subTotal = 0

for i in range(len(calc)):
    if calc[i].isdigit():
        nums.append(int(calc[i]))
    else:
        b = nums.pop()
        a = nums.pop()

        if calc[i] == "+":
            subTotal = a + b
        elif calc[i] == "-":
            subTotal = a - b
        elif calc[i] == "*":
            subTotal = a * b
        
        nums.append(subTotal)

print(" ".join(map(str,nums)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
import functools
N = input().split()
stack=[]

for i in N:
    if i.isdigit():
        stack.append(int(i))
        
    else:
        key=0
        if i=="+":
            p2=stack.pop()
            p1=stack.pop()
            stack.append(p1+p2)
        elif i=="-":
            p2=stack.pop()
            p1=stack.pop()
            stack.append(p1-p2)
        elif i=="*":
            p2=stack.pop()
            p1=stack.pop()
            stack.append(p1*p2)
print(stack[0])

Submission status: Accepted

Solution code:
operand = ["+", "-", "*"]
src = [x if x in operand else int(x) for x in  input().split(" ")]
stack = []

for s in src:
    if isinstance(s, int):
        stack.append(s)
    elif s == "+":
       b, a = stack.pop(), stack.pop()
       stack.append(a+b)

    elif s == "-":
        b, a = stack.pop(), stack.pop()
        stack.append(a-b)
    elif s == "*":
        b, a = stack.pop(), stack.pop()
        stack.append(a*b)

print(stack[0])

Submission status: Accepted

Solution code:
operand = ["+", "-", "*"]
src = [x if x in operand else int(x) for x in  input().split(" ")]
stack = []

for s in src:
    if isinstance(s, int):
        stack.append(s)
        continue

    b, a = stack.pop(), stack.pop()

    if s == "+":
       stack.append(a+b)
    elif s == "-":
        stack.append(a-b)
    elif s == "*":
        stack.append(a*b)

print(stack[0])

Submission status: Accepted

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

import sys
import os


lst = input().split()
stack = []

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

Submission status: Accepted

Solution code:
Sum = input().split()
cou = 1
while len(Sum) != 1 :
    cou += 1
    if Sum[cou] == '+' :
        del Sum[cou]
        Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) + int(Sum.pop(cou - 2)))
        cou -= 2
    elif Sum[cou] == '-' :
        del Sum[cou]
        Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) - int(Sum.pop(cou - 2)))
        cou -= 2
    elif Sum[cou] == '*' :
        del Sum[cou]
        Sum.insert((cou - 2) ,int(Sum.pop(cou - 2)) * int(Sum.pop(cou - 2)))
        cou -= 2

print(Sum[0])

Submission status: Accepted

Solution code:
# N = input()
NArray = []
operator = []
operand = []
for x in raw_input().split():
  if  x == '+':
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 + a1)
  elif  x == '-':
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 - a1)
  elif  x == '*':
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 * a1)
  else:
    operand.append(int(x))

print operand[0]

Submission status: Accepted

Solution code:
# coding:utf-8

cal = {
       "+": lambda a, b: b+a,
       "-": lambda a, b: b-a,
       "*": lambda a, b: b*a
       }

stack = []

for i in input().split():
    if i in cal:
        stack.append(cal[i](stack.pop(), stack.pop()))
    else:
        stack.append(int(i))

print(stack[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# N = input()
operand = []
for x in raw_input().split():
  if  x == '+' :
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 + a1)
  elif  x == '-':
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 - a1)
  elif  x == '*':
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    operand.append(a2 * a1)
  else:
    operand.append(int(x))

print operand[0]

Submission status: Accepted

Solution code:
# N = input()
operand = []
for x in raw_input().split():
  if x.isdigit():
    operand.append(int(x))
  else:
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    if  x == '+' :
      operand.append(a2 + a1)
    elif  x == '-':
      operand.append(a2 - a1)
    elif  x == '*':
      operand.append(a2 * a1)


print operand[0]

Submission status: Accepted

Solution code:
operand = []
for x in raw_input().split():
  if x.isdigit():
    operand.append(int(x))
  else:
    a1 = operand[len(operand)-1]
    operand.pop()
    a2 = operand[len(operand)-1]
    operand.pop()
    if  x == '+' :
      operand.append(a2 + a1)
    elif  x == '-':
      operand.append(a2 - a1)
    elif  x == '*':
      operand.append(a2 * a1)

print operand[0]

Submission status: Accepted

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

stack = []
for op in s:
	if op.isdigit():
		stack.append(op)
	else:
		b = int(stack.pop())
		a = int(stack.pop())
		ret = 0
		if op == "+":
			ret = a + b
		elif op == "-":
			ret = a - b
		elif op == "*":
			ret = a * b
		stack.append(str(ret))
print stack.pop()

Submission status: Accepted

Solution code:
operand = []
for x in raw_input().split():
  if x.isdigit():
    operand.append(int(x))
  else:
    a1 = operand.pop()
    a2 = operand.pop()
    if  x == '+' :
      operand.append(a2 + a1)
    elif  x == '-':
      operand.append(a2 - a1)
    elif  x == '*':
      operand.append(a2 * a1)

print operand.pop()

Submission status: Accepted

Solution code:
input_ = input()
operators = input_.split(' ')

stack = []

def operate(b, a, op):
    return {
        '+': a+b,
        '-': a-b,
        '*': a*b,
    }.get(op)

for op in operators:
    if op in ('+','-','*'):
        stack.append(operate(stack.pop(), stack.pop(), op))
    else:
        stack.append(int(op))

print(stack.pop())

Submission status: Accepted

Solution code:
ops = [op for op in input().split(" ")]
stack = []
for op in ops:
    if op == "+":
        stack = stack[:-2]+[stack[-2]+stack[-1]]
    elif op == "-":
        stack = stack[:-2]+[stack[-2]-stack[-1]]
    elif op == "*":
        stack = stack[:-2]+[stack[-2]*stack[-1]]
    else:
        stack.append(int(op))
print(stack[0])

Submission status: Accepted

Solution code:
#4.2
a = list(input().split())

stack = [0] * 1000
stackIndex = -1

for ele in a:
    if ele != '+' and ele != '-' and ele != '*':
        stackIndex += 1
        stack[stackIndex] = ele
    else:
        num2 = int(stack[stackIndex])
        stackIndex -= 1
        num1 = int(stack[stackIndex])
        stackIndex -= 1
        
        calcVal = 0
        if ele == '+':
            calcVal = num1 + num2
        elif ele == '-':
            calcVal = num1 - num2
        elif ele == '*':
            calcVal = num1 * num2
        
        stackIndex += 1
        stack[stackIndex] = calcVal

print(stack[stackIndex])

Submission status: Accepted

Solution code:
def Cal(sym,n1,n2):
    n1 = int(n1)
    n2 = int(n2)
    if sym == '+':
        ans = n1 + n2
    elif sym == '-':
        ans = n1 - n2
    else:
        ans = n1 * n2
        
    return ans

stack = []

for item in raw_input().split():
    if item.isdigit():
        stack.append(item)
    else:
        n2 = stack.pop()
        n1 = stack.pop()
        sym = item
        ans = Cal(sym,n1,n2)
        stack.append(ans)

Ans = stack.pop()

print Ans

Submission status: Accepted

Solution code:
def min(a,b,c):
    if a<b and a<c:
        return a
    elif b<c:
        return b
    else:
        return c


def calc(x):
    try:
        a=x.index("+")
    except:
        a=300
    try:
        b=x.index("-")
    except:
        b=300
    try:
        c=x.index("*")
    except:
        c=300

    ind=min(a,b,c)

    if ind==300:
        return x[0]
    
    if x[ind]=="+":
        x.insert(ind-2,int(x[ind-2])+int(x[ind-1]))
    elif x[ind]=="-":
        x.insert(ind-2,int(x[ind-2])-int(x[ind-1]))
    else:
        x.insert(ind-2,int(x[ind-2])*int(x[ind-1]))
    x.pop(ind-1)
    x.pop(ind-1)
    x.pop(ind-1)
    return calc(x)


operas=input().split(" ")
print (calc(operas))

Submission status: Accepted

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


a = [e for e in input().split()]
# a = ['1', '2', '+', '3', '4','-', '*']
# a = ['1', '2', '+']
l = []


for x in a:

    if x not in ['+', '-', '*']:
        x = int(x)
        l.append(x)
    else:
        a = l.pop()
        b = l.pop()

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


print(sum(l))

Submission status: Accepted

Solution code:
def RPN(states):

        operator = {
           '+' :(lambda x, y: x + y),
           '-' :(lambda x, y: x - y),
           '*' :(lambda x, y: x * y),
           '/' :(lambda x, y: float(x)/y)
        }
        stack = []
        for index, z in enumerate(states):
                if z not in operator.keys ():
                        stack.append(int(z))
                        continue
                y = stack.pop()
                x = stack.pop()
                stack.append(operator[z](x,y))
        print (stack[0])
        return (stack[0])

if __name__ == '__main__':
        import sys
        RPN(input().split(" "))

Submission status: Accepted

Solution code:
"RPN using stack for AOJ ALDS1_3_A: Stack"

# coding: utf-8


def rpn(exp):
    """
    rpn calculation
    >>> rpn(['1', '2', '+', '3', '4', '-', '*'])
    -3
    """

    stack = []
    for elem in exp:
        if elem == '+':
            stack.append(int(stack.pop()) + int(stack.pop()))
        elif elem == '-':
            stack.append(-int(stack.pop()) + int(stack.pop()))
        elif elem == '*':
            stack.append(int(stack.pop()) * int(stack.pop()))
        elif elem == '/':
            stack.append(int(stack.pop()) / int(stack.pop()))
        else:
            stack.append(elem)

    print(stack.pop())


def main():
    "main function"

    exp = list(input().split(' '))
    rpn(exp)

if __name__ == '__main__':
    # import doctest
    # doctest.testmod()

    main()