[Tutor] Simple RPN calculator

Gregor Lingl glingl at aon.at
Wed Dec 8 19:52:20 CET 2004



Terry Carroll schrieb:

>On Mon, 6 Dec 2004, Chad Crabtree wrote:
>
>  
>
>>Bob Gailer wrote:
>>
>>    
>>
>>>For grins I just wrote one that takes '1 2.3 - 3 4 5 + * /' as
>>>      
>>>
>>input 
>>    
>>
>>>and prints -0.0481481.... 8 lines of Python. That indeed is less
>>>      
>>>
>>than 
>>    
>>
>>>100. Took about 7 minutes to code and test. 
>>>      
>>>
>>I'm quite interested in seeing the sourcecode for that.
>>    
>>
>
>Me, too.  I'm always interested in cool little Python examples.
>
>  
>
Not having read the thread,  this script prints the same result:

inp = raw_input("Gimmi an RPN-expression: ").split()
i=0
while len(inp)>1:
    while inp[i] not in "+-*/":
        i+=1
    print "**", i, inp     #1 
    inp[i-2:i+1] = [str(eval(inp[i-2]+inp[i]+inp[i-1]))]
    i-=1
    print "***", i, inp    #2
print inp[0]

If you delete the two numbered print statements it also has 8 lines
of code. (They are inserted to show what's going on!)
Excuse me if this posting suffers from redundancy.
I'm very interested in Bob's solution, too
Gregor




>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>


More information about the Tutor mailing list