"""

1 + 2 => 1 2 +
1 + 2 * 3 => 2 3 * 1 +
5 * 5 - 6 / 2 => 5 5 * 6 2 / -
"""

OPS = {'+', '-', '*', '/'}


def apply(op, a, b):
    if op == '+':
        return a + b
    elif op == '-':
        return a - b
    elif op == '*':
        return a * b
    else:
        return float(a) / b


def reverse_polish_notation(s):
    toks = s.split()
    stack = []
    for tok in toks:
        if tok not in OPS:
            stack.append(tok)
        else:
            rhs = stack.pop()
            stack.append(apply(tok, int(stack.pop()), int(rhs)))
    return int(stack[0])



if __name__ == '__main__':
    print reverse_polish_notation('2 1 + 3 *')
