------ art_7276_18935211.1182100665776 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hello, here is my solution for the arithmetic quiz. I am a ruby beginner and this is my first ruby program (ok, my second one, the first was "puts 'hello world'"). So please forgive me the dirty trick using eval (why build an own parser if ruby provides such a fine solution?). I build in a brute force search algorithm via an iterative method for permutations. Therefore, any valid ruby expression is allowed, e.g. verbal_arithmetic.rb 'a+b
&& a+c -b && a*c ' solving a*b*c*d! && a+b
&& a+c -b && a*c -- Solution --- a: 2 b: 1 c: 3 d: 6 Please feel free to send any comments. Regards Holger #!/usr/bin/ruby -w # # Solution to ruby quiz #128 # http://www.rubyquiz.com/quiz128.html # by Holger # # Usage: # verbal_arithmetic.rb <equation> # # Examples: # verbal_arithmetic.rb 'send+more ney' # verbal_arithmetic.rb 'a+b
&& a+c -b && a*c ' # #********************************************************************* # Permutator which gives all combinations of <m> elements out of # array <n> # # usage: # perms(m, n) { |x| ... } # #********************************************************************* def perms(m, n) p nil] * m t -1] * m k while k > if k yield p k -1 end n[t[k]] [k] if t[k]>