From: "Jim Truher" <jtruher@gmail.com>
Date: December 4, 2007 12:56:13 AM CST
To: submission@rubyquiz.com
Subject: Please Forward: Ruby Quiz Submission (#148)Good quiz this week, I had time to do it! Looks like I wasn't the only one, either. Some reasonably graceful error handling if your operator/operand count is off.
#Ruby quiz #148
require 'pp'class Quiz148
def initialize(str)
@regexed_string = str.scan(/\d+\.\d*|[-*+\/]|\d*/)
@regexed_string.reject! {|i| i == "" }
@num_stack =postfix_to_infix
enddef postfix_to_infix
@regexed_string.each do |c|
case c
when "*"
if check_validity
one,two = eval_parens(@num_stack.pop), @num_stack.pop
@num_stack.push(two + c +one)
end
when "/"
if check_validity
one,two = eval_parens(@num_stack.pop), eval_parens(@num_stack.pop)
@num_stack.push(two + c +one)
end
when "+", "-"
if check_validity
one,two = @num_stack.pop, @num_stack.pop
@num_stack.push(two + c + one)
end
else # - we've got a number!
@num_stack.push(c)
end # - case c
end # - @regexed_string.each do |c|
enddef eval_parens(str)
if (str.include? "*") || (str.include? "+") || (str.include? "-") || (str.include? "/")
"(" + str + ")"
else
str
end
enddef check_validity
if @num_stack.size == 1
puts "Oh no, looks like your expression has too many operators?! Breaking early!"
return false
end
return true
enddef to_s
if @num_stack.size > 1
puts "Looks like your expression has too many operandsGonna just print what we've got"
end
@num_stack.first.to_s
end
end
#A few tests!
if ARGV[0]
puts Quiz148.new(ARGV[0]).to_s
end
puts Quiz148.new("56 34 213.7 + * 678 - +").to_s
puts Quiz148.new("3 5 * 5 8 * /").to_s
puts Quiz148.new("3 5 * 5 ").to_s
···
Begin forwarded message: