Your job is to create a calculator which evaluates expressions in [Reverse Polish notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

For example expression `5 1 2 + 4 * + 3 -` (which is equivalent to `5 + ((1 + 2) * 4) - 3` in normal notation) should evaluate to `14`.

For your convenience, the input is formatted such that a space is provided between every token.

Empty expression should evaluate to `0`.

Valid operations are `+`, `-`, `*`, `/`.

You may assume that there won't be exceptional situations (like stack underflow or division by zero).

Difficulty: EASY
Tags: String algorithms

Mathematics

Starter code:
def calc(expr):
	

Solutions:
import operator

def calc(expr):
	OPERATORS = {'+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.truediv}
	stack = [0]
	for token in expr.split(' '):
		if token in OPERATORS:
			(op2, op1) = (stack.pop(), stack.pop())
			stack.append(OPERATORS[token](op1, op2))
		elif token:
			stack.append(float(token))
	return stack.pop()


operator = set(['+', '-', '*', '/'])

def calc(expr):
	stack = list()
	for c in expr.split():
		if c in operator:
			first = stack.pop()
			second = stack.pop()
			stack.append(str(eval(second + c + first)))
		else:
			stack.append(c)
	return eval(stack.pop()) if stack else 0


def calc(expr):
	if len(expr.strip()) < 1:
		return 0
	if expr.count('+') == 0 and expr.count('-') == 0 and (expr.count('*') == 0) and (expr.count('/') == 0):
		return float(expr.split(' ')[-1])
	op_flags = '+-*/'
	nums = []
	ops = []
	expr_list = expr.split(' ')
	for e in expr_list:
		if e in op_flags:
			b = float(nums.pop())
			a = float(nums.pop())
			if e == '+':
				nums.append(a + b)
			elif e == '-':
				nums.append(a - b)
			elif e == '*':
				nums.append(a * b)
			elif e == '/':
				nums.append(a / b)
		else:
			nums.append(e)
	return nums[0]


OPS = {'+': lambda x, y: y + x, '-': lambda x, y: y - x, '*': lambda x, y: y * x, '/': lambda x, y: y / x}

def calc(expr):
	if not expr:
		return 0
	stack = []
	[stack.append(*map(OPS[sign], [stack.pop()], [stack.pop()])) if sign in OPS else stack.append(float(sign)) for sign in expr.split(' ')]
	return stack[-1]


def calc(s):
	ops = {'+': lambda x, y: x + y, '-': lambda x, y: x - y, '*': lambda x, y: x * y, '/': lambda x, y: x - y}
	pre = [0]
	for i in s.split():
		if i in ops:
			(b, a) = (pre.pop(), pre.pop())
			pre.append(ops[i](a, b))
		else:
			pre.append(float(i))
	return pre.pop()


def calc(expr):
	oplist = []
	if expr == '':
		return 0
	for op in expr.split(' '):
		try:
			op = float(op)
			oplist.append(op)
		except ValueError:
			op2 = oplist.pop()
			op1 = oplist.pop()
			oplist.append(eval(str(op1) + op + str(op2)))
	return oplist.pop()


ops = {'+': lambda x, y: x + y, '-': lambda x, y: x - y, '*': lambda x, y: x * y, '/': lambda x, y: x / y}

def calc(expr):
	(stack, tokens) = ([], expr.split(' ') if expr != '' else ['0'])
	for token in tokens:
		stack.append(float(token)) if token not in ops else stack.append(ops[token](*(stack.pop(), stack.pop())[::-1]))
	return stack[-1]


def calc(expr):
	stack = []
	[stack.append(eval('{1}{2}{0}'.format(stack.pop(), stack.pop(), e)) if e in ('+', '-', '/', '*') else e) for e in expr.split()]
	return float(stack.pop()) if stack else 0


def calc(expr):
	print(expr)
	s = [0]
	for arg in expr.split(' '):
		try:
			s.append(float(arg))
		except:
			try:
				s.append(eval(f'{s.pop(-2)} {arg} {s.pop(-1)}'))
			except:
				pass
	return s[-1]