# Fwd: Ruby Quiz #148

**URL:** <https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936>\
**Category:** Ruby\
**Created:** [December 3, 2007, 3:48am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936 "2007-12-03T03:48:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@daveh0](https://www.ruby-forum.com/u/daveh0)\
**Post date:** [December 3, 2007, 3:48am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936/1 "2007-12-03T03:48:04Z")

</div>

Solution to quiz #148.  
Thanks,

-Dave  
---------- Forwarded message ----------  
From: Dave P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Nov 30, 2007 7:26 PM  
Subject: Ruby Q. #148  
To: removed\_email\_address@domain.invalid

#!/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

---

<div class="post-metadata">

**Author:** ![daveh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@daveh0](https://www.ruby-forum.com/u/daveh0)\
**Post date:** [December 3, 2007, 4:17am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936/2 "2007-12-03T04:17:12Z")

</div>

I’ve looked at a lot of solutions for this quiz today and I have to say  
I like yours the most. Good work 🙂

---

<div class="post-metadata">

**Author:** ![daveh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@daveh0](https://www.ruby-forum.com/u/daveh0)\
**Post date:** [December 3, 2007, 5:12am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936/3 "2007-12-03T05:12:13Z")

</div>

On Dec 2, 2007, at 9:17 PM, Michael B. wrote:

> I’ve looked at a lot of solutions for this quiz today and I have to  
> say  
> I like yours the most. Good work 🙂

Since there was no name specified, I think all solvers should assume  
he was talking to you. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![daveh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@daveh0](https://www.ruby-forum.com/u/daveh0)\
**Post date:** [December 3, 2007, 5:41am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936/4 "2007-12-03T05:41:25Z")

</div>

On Sun, 02 Dec 2007 21:47:29 -0500, Dave P. wrote:

> To: removed\_email\_address@domain.invalid  
> “5 9 \* 8 7 4 6 + \* 2 1 3 \* + \* + \*”]  
> stack.push “#{first} #{c} #{second}”  
> 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

---

<div class="post-metadata">

**Author:** ![daveh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@daveh0](https://www.ruby-forum.com/u/daveh0)\
**Post date:** [December 3, 2007, 8:46am UTC](https://www.ruby-forum.com/t/fwd-ruby-quiz-148/121936/5 "2007-12-03T08:46:45Z")

</div>

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©  
else  
second, first = stack.pop, stack.pop  
stack.push “( #{first} #{c} #{second} )”  
end  
end  
puts “postfix = #{postfix}”  
puts “infix = #{stack.pop}”  
puts  
end
