fork download
  1. class RPN:
  2. def __init__(self):
  3. self.stack = []
  4. self.result = 0
  5.  
  6. def push(self, data):
  7. self.stack.append(float(data))
  8.  
  9. def pop(self):
  10. return self.stack.pop(len(self.stack)-1)
  11.  
  12. def operation(self, op):
  13. term1 = self.pop()
  14. term2 = self.pop()
  15. self.result = eval('%f%c%f'%(term2,op,term1))
  16. self.push(self.result)
  17.  
  18. def eval(self,str):
  19. print("Evaluating",str)
  20. for ch in str.split(" "):
  21. if ch in '+-*/':
  22. self.operation(ch)
  23. else:
  24. self.push(ch)
  25. return self.result
  26. def main():
  27. c = RPN()
  28. print(c.eval("1 2 3 4 5 + + + +"))
  29. main()
  30.  
Success #stdin #stdout 0.03s 9816KB
stdin
Standard input is empty
stdout
Evaluating 1 2 3 4 5 + + + +
15.0