From: James Edward Gray II Date: 2007-12-03T01:45:52+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1--736578764 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: Daniel Lindsley > Date: December 1, 2007 12:53:48 AM CST > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > James, > > > I have a submission for Ruby Quiz #148: "Postfix to Infix". It is > neither amazing nor beautiful but it seems to work and I've hit my > limit on time I can spend on it. I'd appreciate any feedback if > it's appropriate to request it. And my apologies if a different > packaging format or different means of bundling the code together > is preferred (I'm rather new to all of this). Thank you for the > excellent site and challenges! > > > Daniel > > --Apple-Mail-1--736578764 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name=postfix_to_infix.rb Content-Disposition: attachment; filename=postfix_to_infix.rb class PostfixToInfix OPERATORS = %w{* / + -} def initialize(postfix) raise "Requires a postfix expression as a string." unless postfix.class == String raise "Requires a valid postfix expression." unless postfix.split.length >= 3 @postfix = postfix end def convert(verbose=true) postfix_terms = @postfix.split if verbose convert_verbose(postfix_terms) else raise NotImplementedError("Because I'm pressed for time.") end end def convert_verbose(postfix_terms) infix_terms = [] while not postfix_terms.empty? current_term = postfix_terms.shift if OPERATORS.include? current_term # Perform the old switcheroo. right = infix_terms.pop left = infix_terms.pop current_term = "(#{left} #{current_term} #{right})" end infix_terms.push current_term end return infix_terms.shift end end if __FILE__ == $0 unless ARGV.length == 1 puts "Usage: #{$PROGRAM_NAME} " exit end ps2i = PostfixToInfix.new(ARGV.shift) puts ps2i.convert end --Apple-Mail-1--736578764 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name=postfix_to_infix_test.rb Content-Disposition: attachment; filename=postfix_to_infix_test.rb require 'test/unit' require 'postfix_to_infix' class PostfixToInfixTest < Test::Unit::TestCase VALID_CASES = [ # { :postfix => '', :infix_verbose => '', :infix_preferred => '' } { :postfix => '2 3 -', :infix_verbose => '(2 - 3)', :infix_preferred => '2 - 3' }, { :postfix => '2 3 5 + *', :infix_verbose => '(2 * (3 + 5))', :infix_preferred => '2 * (3 + 5)' }, { :postfix => '56 34 213.7 + * 678 -', :infix_verbose => '((56 * (34 + 213.7)) - 678)', :infix_preferred => '56 * (34 + 213.7) - 678' }, { :postfix => '1 56 35 + 16 9 - / +', :infix_verbose => '(1 + ((56 + 35) / (16 - 9)))', :infix_preferred => '1 + (56 + 35) / (16 - 9)' }, ] def test_initialize_without_parameter_should_raise_exception assert_raise(ArgumentError) { PostfixToInfix.new } end def test_initialize_with_incorrect_parameter_should_raise_exception assert_raise(RuntimeError) { PostfixToInfix.new(1) } assert_raise(RuntimeError) { PostfixToInfix.new([]) } assert_raise(RuntimeError) { PostfixToInfix.new({}) } end def test_convert_sample_cases_verbose VALID_CASES.each do |sample| assert_nothing_raised(Exception) { ps2i = PostfixToInfix.new(sample[:postfix]) } ps2i = PostfixToInfix.new(sample[:postfix]) assert_equal(sample[:infix_verbose], ps2i.convert) end end end --Apple-Mail-1--736578764 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1--736578764--