Postfix to Infix, Part 2
Posted by Alpha Tue, 04 Dec 2007 06:55:00 GMT
After seeing Christian von Kleist’s solution, I couldn’t help but play with it a bit to come up with this:
#!/usr/bin/env ruby puts ARGV[0].split(/\s*/).inject([]) {|n,i| n << ((%w[+ - * /].include?(i)) ? (b,a=n.pop,n.pop; "(#{a} #{i} #{b})") : i) }[0]