From: Heesob Park Date: 2008-09-05T15:02:49+09:00 Subject: Re: Need a 40 LOC (ignoring comments) to be shorter -- suggestions wanted 2008/9/5 RichardOnRails : > Hi All, > > My middle-school granddaughter posed the following challenge to me: > > Within 10 minutes, Use the digits 3 to 7 in any order > to form a three-digit multiplicand and a two-digit multiplier > such that their product is minimal. > > Out of curiosity as to whether my guess was right, > and with a view toward eliciting an interest in her > for Ruby programming, I wrote the program posted at > http://www.pastie.org/266447 > > The program reveals that I was wrong, but it took me about 40 lines of > code, > ignoring comments. That would probably overwhelm her. I was hoping > that > the code could be condensed with Ruby-isms. BTW, I left some put's in > there > intended to give her a sense of the programs functionality. > > Any ideas/suggestions? > Here is a simple and stupid solution. a = [3,4,5,6,7] d = {} (a[0,3].to_s.to_i .. a.reverse[0,3].to_s.to_i).each{|i| (a[0,2].to_s.to_i .. a.reverse[0,2].to_s.to_i).each{|j| d[[i,j]]=i*j if a.to_s==((i.to_s+j.to_s).split(//).sort.to_s) } } p d.sort{|a,b| a[1]<=>b[1]}[0] Regards, Park Heesob