From: "list. rb" Date: 2007-09-06T15:53:21+09:00 Subject: Backing into numbers with Ruby? ------=_Part_27822_12978600.1189061599918 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Are there any apps out there that can try and arrive at n given x criterion? If not, perhaps there is good reason(complexity maybe?). If there is or if anyone has thoughts to share on the topic, I'm very interested. To exemplify what I'm looking for, here's a 3am(yawns) demonstration: include Math def add(*x) ans = 0 x.first.each {|item| ans += item } return {:calc => x.join("+"), :ans => ans} end def multiply(*x) ans = 1 x.first.each {|item| ans = ans*item } return {:calc => x.join("*"), :ans => ans} end def arrive_at(n, criterion) criterion[:methods].each {|m| response = send(m, criterion[:given]) puts response[:calc] if response[:ans].to_s == n.to_s i=0 criterion[:lambdas].each {|lamb| if lamb[:for].include? m lamb[:try].each {|l| lambda_response = l.call(response) puts "#{m}[lambda[#{i.to_s}]](#{response[:calc]})" if lambda_response.to_s == n.to_s } end i+=1 } } end arrive_at(10, {:given => [2,5], :methods => ['multiply'], :lambdas => []}) # => yields "2*5" arrive_at(10, {:given => [7,3], :methods => ['add', 'multiply'], :lambdas => []}) # => yields "7+3" arrive_at(12.5663706143592, { :given => [2, 2], :methods => ['add', 'multiply'], :lambdas => [{:for => ['multiply'], :try => [lambda {|x| x[:ans]*PI}]}] } ) # => "multiply[lambda[0]](2*2)" ------=_Part_27822_12978600.1189061599918--