Diversion

A little Python snippet, good for a diversion. I couldn’t fit it into a tweet though:

s=[];op=dict((o,eval('lambda a,b:b%sa'%o)) for o in '+-*/%')
while not any(s.append(op[w](s.pop(),s.pop())if w in op 
else float(w)) for w in raw_input().split()):
 print '=',s[-1]

Just a simple 4-function RPN calculator, accepting input from console:

 1
 = 1.0
 2 2 +
 = 4.0
 1 2 -
 = -1.0
 2 3 * 1 +
 = 7.0
 2 3 1 * +
 = 5.0

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.