From: Dave Pederson Date: 2007-12-03T16:46:07+09:00 Subject: Re: Fwd: Ruby Quiz #148 ------=_Part_12863_22424324.1196667969937 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks for pointing that out. I was trying to minimize parentheses...looks like i kinda hurried though it. Anyways, here was my first solution (minus the bonus points): #!/usr/bin/env ruby op = %w{ + - * / } postfixes = ["2 3 5 + *", "1 56 35 + 16 9 - / +", "56 34 213.7 + * 678 -", "5 9 * 8 7 4 6 + * 2 1 3 * + * + *", "3 5 * 5 8 * /"] postfixes.each do |postfix| stack = [] postfix.split.each do |c| unless op.include? c stack.push(c) else second, first = stack.pop, stack.pop stack.push "( #{first} #{c} #{second} )" end end puts "postfix = #{postfix}" puts "infix = #{stack.pop}" puts end On Dec 2, 2007 8:40 PM, Ken Bloom wrote: > On Sun, 02 Dec 2007 21:47:29 -0500, Dave Pederson wrote: > > > Note: parts of this message were removed by the gateway to make it a > > legal Usenet post. > > > > Solution to quiz #148. > > Thanks, > > > > -Dave > > ---------- Forwarded message ---------- From: Dave Pederson > > Date: Nov 30, 2007 7:26 PM > > Subject: Ruby Quiz #148 > > To: dave.pederson.ruby@gmail.com > > > > > > #!/usr/bin/env ruby > > > > op = %w{ + - / * } > > pm = %w{ + - } > > > > postfixes = ["2 3 5 + *", > > "1 56 35 + 16 9 - / +", > > "56 34 213.7 + * 678 -", > > "5 9 * 8 7 4 6 + * 2 1 3 * + * + *"] > > > > puts > > postfixes.each do |postfix| > > stack = [] > > postfix.split.each do |c| > > if op.include?(c) > > second, first = stack.pop, stack.pop > > if pm.include?(c) > > stack.push "(#{first} #{c} #{second})" > > else > > stack.push "#{first} #{c} #{second}" > > end > > else > > stack.push(c) > > end > > end > > infix = stack.pop > > if (infix[0] == 40 && infix[infix.size-1] == 41) > > infix = infix[1, infix.size-2] > > end > > puts "postfix = #{postfix}" > > puts "infix = #{infix}" > > puts > > end > > This is incorrect: > > postfix = 3 5 * 5 8 * / > infix = 3 * 5 / 5 * 8 > > It should be > infix = 3 * 5 / (5 * 8) > > --Ken > > -- > Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. > Department of Computer Science. Illinois Institute of Technology. > http://www.iit.edu/~kbloom1/ > > ------=_Part_12863_22424324.1196667969937--