class RPN:
def __init__(self):
self.stack = []
self.result = 0
def push(self, data):
self.stack.append(float(data))
def pop(self):
return self.stack.pop(len(self.stack)-1)
def operation(self, op):
term1 = self.pop()
term2 = self.pop()
self.result = eval('%f%c%f'%(term2,op,term1))
self.push(self.result)
def eval(self,str):
print("Evaluating",str)
for ch in str.split(" "):
if ch in '+-*/':
self.operation(ch)
else:
self.push(ch)
return self.result
def main():
c = RPN()
print(c.eval("1 2 3 4 5 + + + +"))
main()
Y2xhc3MgUlBOOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYuc3RhY2sgPSBbXQogICAgICAgIHNlbGYucmVzdWx0ID0gMAoKICAgIGRlZiBwdXNoKHNlbGYsIGRhdGEpOgogICAgICAgIHNlbGYuc3RhY2suYXBwZW5kKGZsb2F0KGRhdGEpKQoKICAgIGRlZiBwb3Aoc2VsZik6CiAgICAgICAgcmV0dXJuIHNlbGYuc3RhY2sucG9wKGxlbihzZWxmLnN0YWNrKS0xKQoKICAgIGRlZiBvcGVyYXRpb24oc2VsZiwgb3ApOgogICAgICAgIHRlcm0xID0gc2VsZi5wb3AoKQogICAgICAgIHRlcm0yID0gc2VsZi5wb3AoKSAgICAgICAgCiAgICAgICAgc2VsZi5yZXN1bHQgPSBldmFsKCclZiVjJWYnJSh0ZXJtMixvcCx0ZXJtMSkpCiAgICAgICAgc2VsZi5wdXNoKHNlbGYucmVzdWx0KQoKICAgIGRlZiBldmFsKHNlbGYsc3RyKToKICAgICAgICBwcmludCgiRXZhbHVhdGluZyIsc3RyKQogICAgICAgIGZvciBjaCBpbiBzdHIuc3BsaXQoIiAiKToKICAgICAgICAgICAgaWYgY2ggaW4gJystKi8nOgogICAgICAgICAgICAgICAgc2VsZi5vcGVyYXRpb24oY2gpCiAgICAgICAgICAgIGVsc2U6CiAgICAgICAgICAgICAgICBzZWxmLnB1c2goY2gpCiAgICAgICAgcmV0dXJuIHNlbGYucmVzdWx0CmRlZiBtYWluKCk6CiAgICBjID0gUlBOKCkKICAgIHByaW50KGMuZXZhbCgiMSAyIDMgNCA1ICsgKyArICsiKSkKbWFpbigpCg==