Reverse Polish notation
Ruby - 95 77 characters
a=[]
gets.split.each{|b|a<<(b=~/\d/?b.to_f: (j,k=a.pop 2;j.send b,k))}
p a[0]
Takes input on stdin.
Testing code
[
"-4 5 +",
"5 2 /",
"5 2.5 /",
"5 1 2 + 4 * 3 - +",
"4 2 5 * + 1 3 2 * + /",
"12 8 3 * 6 / - 2 + -20.5 "
].each do |test|
puts "[#{test}] gives #{`echo '#{test}' | ruby golf-polish.rb`}"
end
gives
[-4 5 +] gives 1.0
[5 2 /] gives 2.5
[5 2.5 /] gives 2.0
[5 1 2 + 4 * 3 - +] gives 14.0
[4 2 5 * + 1 3 2 * + /] gives 2.0
[12 8 3 * 6 / - 2 + -20.5 ] gives 10.0
Unlike the C version this returns the last valid result if there are extra numbers appended to the input it seems.
Scheme, 162 chars
(Line breaks added for clarity—all are optional.)
(let l((s'()))(let((t(read)))(cond((number? t)(l`(,t,@s)))((assq t
`((+,+)(-,-)(*,*)(/,/)))=>(lambda(a)(l`(,((cadr a)(cadr s)(car s))
,@(cddr s)))))(else(car s)))))
Fully-formatted (ungolfed) version:
(let loop ((stack '()))
(let ((token (read)))
(cond ((number? token) (loop `(,token ,@stack)))
((assq token `((+ ,+) (- ,-) (* ,*) (/ ,/)))
=> (lambda (ass) (loop `(,((cadr ass) (cadr stack) (car stack))
,@(cddr stack)))))
(else (car stack)))))
Selected commentary
`(,foo ,@bar)
is the same as (cons foo bar)
(i.e., it (effectively†) returns a new list with foo
prepended to bar
), except it's one character shorter if you compress all the spaces out.
Thus, you can read the iteration clauses as (loop (cons token stack))
and (loop (cons ((cadr ass) (cadr stack) (car stack)) (cddr stack)))
if that's easier on your eyes.
`((+ ,+) (- ,-) (* ,*) (/ ,/))
creates an association list with the symbol +
paired with the procedure +
, and likewise with the other operators. Thus it's a simple symbol lookup table (bare words are (read)
in as symbols, which is why no further processing on token
is necessary). Association lists have O(n) lookup, and thus are only suitable for short lists, as is the case here. :-P
† This is not technically accurate, but, for non-Lisp programmers, it gets a right-enough idea across.
Python - 124 chars
s=[1,1]
for i in raw_input().split():b,a=map(float,s[:2]);s[:2]=[[a+b],[a-b],[a*b],[a/b],[i,b,a]]["+-*/".find(i)]
print s[0]
Python - 133 chars
s=[1,1]
for i in raw_input().split():b,a=map(float,s[:2]);s={'+':[a+b],'-':[a-b],'*':[a*b],'/':[a/b]}.get(i,[i,b,a])+s[2:]
print s[0]