fork download
  1. import math
  2.  
  3. def RPN_calc(list_calc_exp):
  4.  
  5. operators = {'+': lambda b,a: a+b,
  6. '-': lambda b,a: a-b,
  7. '*': lambda b,a: a*b,
  8. '/': lambda b,a: a/b,
  9. '//': lambda b,a: a//b,
  10. '%': lambda b,a: a%b,
  11. '^': lambda b,a: a**b,
  12. '!': lambda x: math.factorial(x)}
  13.  
  14.  
  15. for ind, char in enumerate(list_calc_exp):
  16. if char == '!':
  17. list_calc_exp[ind-1] = operators[char](float(list_calc_exp[ind-1]))
  18. del list_calc_exp[ind]
  19. return list_calc_exp
  20. elif char in operators:
  21. list_calc_exp[ind-2] = operators[char](float(list_calc_exp[ind-1]), float(list_calc_exp[ind-2]))
  22. del list_calc_exp[ind]
  23. del list_calc_exp[ind-1]
  24. return list_calc_exp
  25.  
  26. def output_res(text):
  27.  
  28. result = text.split()
  29.  
  30. while len(result) != 1:
  31. result = RPN_calc(result)
  32. return result[0]
  33.  
  34.  
  35. print(output_res('0.5 1 2 ! * 2 1 ^ + 10 + *'))
  36. print(output_res('1 2 3 4 ! + - / 100 *'))
  37. print(output_res('100 807 3 331 * + 2 2 1 + 2 + * 5 ^ * 23 10 558 * 10 * + + *'))
Success #stdin #stdout 0s 23304KB
stdin
Standard input is empty
stdout
7.0
-4.0
18005582300.0