From: Ken Bloom Date: 2007-04-11T12:10:11+09:00 Subject: Re: Fwd: Please Forward: Ruby Quiz Submission On Mon, 09 Apr 2007 11:11:34 +0900, James Edward Gray II wrote: > Begin forwarded message: > >> From: Dennis Frommknecht 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 >> >> > #!/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 = 100 > OPS = [['+', '-', '-'], > ['-', '+', '-'], > ['-', '-', '+']] > > num_of_ops = OPS[0].length > equ_counter = 0 > > 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 = > eval command > equ_counter += 1 > 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" Way to go with the partition generator. I don't do nearly as much crazy stuff with regular expressions as I should. --Ken -- Ken Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/