--Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset -ASCII; formatðïwed Begin forwarded message: > From: Dennis Frommknecht <dennis / frommknecht.net> > Date: April 8, 2007 12:26:42 PM CDT > To: submission / rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Hi, > > here is a solution for quiz number 119. > As I'm not subscribed to the mailing list, i hope you can forward it. > > Thanks, > > Dennis Frommknecht > > --Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode55; name zzlers.rb Content-Disposition: attachment; filename zzlers.rb #!/usr/bin/env ruby -W # This solution uses 3 nested loops to divide the # numbers into 4 groups (using regular expressions). # Then the 3 allowed combinations of plus and minus # are inserted between the groups. # Finally the result is calculated using eval NUMBERS 123456789" CORRECT_RES 00 OPS ['+', '-', '-'], ['-', '+', '-'], ['-', '-', '+']] num_of_ops PS[0].length equ_counter 1.upto(NUMBERS.length - num_of_ops) do |i| 1.upto(NUMBERS.length - num_of_ops - i + 1) do |j| 1.upto(NUMBERS.length - num_of_ops - i + 1 - j + 1) do |k| if NUMBERS.match(/(\d{#{i}})(\d{#{j}})(\d{#{k}})(\d+)/) then OPS.each do |o| command #{$1} #{o[0]} #{$2} #{o[1]} #{$3} #{o[2]} #{$4}" res val command equ_counter + puts "*" * 15 if res CORRECT_RES puts "#{command} {res}" puts "*" * 15 if res CORRECT_RES end end end end end puts "#{equ_counter} possible equations tested" --Apple-Mail-1-208995236 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset -ASCII; formatðïwed --Apple-Mail-1-208995236--