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]

Posted in ,  | no comments | no trackbacks

Comments

Trackbacks

Use the following link to trackback from your own site:
http://blog.kejadlen.net/trackbacks?article_id=postfix-to-infix-part-2&day=03&month=12&year=2007

(leave url/email »)

   Comment Markup Help Preview comment