Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Postfix to Infix (#148)

X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.71.11
Path: g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!nntpfeed.proxad.net!news.netfinity.fr!talisker.lacave.net!lacave.net!not-for-mail
From: Sharon Phillips <phillip...@yahoo.co.uk>
Newsgroups: comp.lang.ruby
Subject: Re: [QUIZ] Postfix to Infix (#148)
Date: Tue, 4 Dec 2007 07:58:01 -0500
Organization: Service de news de lacave.net
Lines: 47
Message-ID: <C4BDC09A-38CE-4F12-BF93-253F7A2FF8BB@yahoo.co.uk>
References: <20071130132821.ZZSN2903.eastrmmtao105.cox.net@eastrmimpo01.cox.net>
NNTP-Posting-Host: bristol.highgroove.com
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
X-Trace: talisker.lacave.net 1196773103 80879 65.111.164.187 (4 Dec 2007 12:58:23 GMT)
X-Complaints-To: abuse@lacave.net
NNTP-Posting-Date: Tue, 4 Dec 2007 12:58:23 +0000 (UTC)
In-Reply-To: <20071130132821.ZZSN2903.eastrmmtao105.cox.net@eastrmimpo01.cox.net>
X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway 
X-Mail-Count: 282009
X-Ml-Name: ruby-talk
X-Rubymirror: Yes
X-Ruby-Talk: <C4BDC09A-38CE-4F12-BF93-253F7A2FF...@yahoo.co.uk>

Hi,

Didn't have much time for this, so here's a partial solution.
What I haven't done is work on ARGV and the bracket removal code is  
pretty basic
produces
1+(56+35)/(16-9) - good
1+(2+(3+4)) - needs work...

Cheers,
Dave


eqn= %w[1 56 35 + 16 9 - / +]
ops= %w[+ - * /]

stack= []

eqn.each do |e|
   if ops.include? e
     b= stack.pop || 0
     a= stack.pop || 0
     if stack.empty?
       stack= [a, e.to_sym, b]
     else
       stack << [a, e.to_sym, b]
     end
   else
     stack << e
   end
end

def disp item, depth
   str=''
   if item.class== Array
     inner= item.inject('') {|sum, e| sum << (disp e, depth+1)}
     inner= "(#{inner})" unless ([:*, :/].include? item[1]) || depth==0
     str << inner
   else
     str << item.to_s
   end
   str
end

puts disp(stack,0)