```On Fri, 30 Nov 2007 08:28:17 -0500, Ruby Quiz wrote:

> The three rules of Ruby Quiz:
>
> 1.  Please do not post any solutions or spoiler discussion for this quiz
> until 48 hours have passed from the time on this message.
>
> 2.  Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3.  Enjoy!
>
> Suggestion:  A [QUIZ] in the subject of emails about the problem helps
> original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=
>
> There are many different ways to write mathematical equations.  Infix
> notation is probably the most popular and yields expressions like:
>
> 	2 * (3 + 5)
>
> Some people like to work with a postfix notation (often called Reverse
> Polish Notation or just RPN) though, which doesn't require parentheses
> for the same equation:
>
> 	2 3 5 + *
>
> You can compare the results of these equations using the Unix utilities
> bc (infix) and dc (postfix):
>
> 	\$ bc <<< '2 * (3 + 5)'
> 	16
> 	\$ dc <<< '2 3 5 + * p'
> 	16
>
> The "p" instruction tacked onto the end of the expression for dc just
> tells it to print the result.
>
> This week's quiz is to write a script that translates postfix
> expressions into the equivalent infix expression.  In the simplest form,
> your script should function as such:
>
> 	\$ ruby postfix_to_infix.rb '2 3 +'
> 	2 + 3
>
> At minimum, try to support the four basic math operators:  +, -, *, and
> /.  Feel free to add others though.  For numbers, remember to accept
> decimal values.
>
> You can count on the postfix expressions having spaces between each
> term, if you like.  While dc is content with 2 3+p, you don't have to
> support it unless you want to.
>
> For an added bonus, try to keep the parentheses added to infix
> expressions to the minimum of what is needed.  For example, prefer these
> results:
>
> 	\$ ruby postfix_to_infix.rb '56 34 213.7 + * 678 -' 56 * (34 +
213.7) -
> 	678
> 	\$ ruby postfix_to_infix.rb '1 56 35 + 16 9 - / +' 1 + (56 + 35) /
(16 -
> 	9)
>
> to these:
>
> 	\$ ruby postfix_to_infix.rb '56 34 213.7 + * 678 -' ((56 * (34 +
213.7))
> 	- 678)
> 	\$ ruby postfix_to_infix.rb '1 56 35 + 16 9 - / +' (1 + ((56 +
35) / (16
> 	- 9)))
>
> Posting equations and your output is not a spoiler.

def convert array
cur=array.pop
case cur
when Numeric: return cur
when String:
rhs=convert(array)
lhs=convert(array)
return "(#{lhs} #{cur} #{rhs})"
end
end

equation=ARGV.split.collect{|x| Integer(x) rescue Float(x) rescue x}
puts convert(equation)

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

```