From: "Rene Koning" <rene.koning@gmail.com>
Date: April 10, 2007 5:52:08 AM CDT
To: submission@rubyquiz.com
Subject: PleaseForward: Ruby QuizSubmissionMy contribution to Quiz 119:
class Operators
OPS = ['','+','-']
attr_reader :operatorsdef initialize(size)
@operators = [0] * size
enddef next_operator(n = 0)
if @operators[n] < (OPS.size - 1)
@operators[n] += 1
else
@operators[n] = 0
next_operator(n + 1)
end
end
endclass Equation
NUM = [1,2,3,4,5,6,7,8,9]
attr_reader :stringdef initialize(operators)
@string = NUM.zip(operators.operators.map { |o| Operators::OPS[o]}.flatten.compact).join('')
enddef answer
eval(@string)
end
endtested = 0
found = 0
match = 100ops = Operators.new(Equation::NUM.size-1)
(Operators::OPS.size**(Equation::NUM.size-1)-1).times do
eq = Equation.new(ops)
answer = eq.answerputs "************************" if answer == match
puts "#{eq.string} = #{answer}"
puts "************************" if answer == matchops.next_operator
tested += 1
found += 1 if answer == match
endputs "#{tested} equations tested, #{found} evaluated to #{match}"
···
Begin forwarded message: