On Wed, 8 Nov 2000, Dave Thomas wrote: > David Alan Black <dblack / candle.superlink.net> writes: > > > I didn't want my first message to the list to consist only of gushing > > (mostly, maybe, but not only), so to keep myself honest I have made > > available a Ruby version of the word-guessing game Jotto, a game which > > I first played on/against a PDP-10 in 1972. > > I like it (i even caught me cheating). Have you considered putting it > up on the Ruby Application Archive under Games? Done! And here are some notes on one part of the implementation which I'm interested in comments and feedback on, toward the goal of Ruby sharpening. The idea here is, given two strings of equal length (we hope), return the number of indices at which they have a letter in common). Example: "shack" and "stash" would return 2 (for 's' and 'a'). The top three candidates that I've found for how to do this efficiently are: def common1(w1, w2) # my favorite, aesthetically :-) (0...w1.size).find_all { |i| w1[i] == w2[i] } .size end def common2(w1, w2) c = 0 (0...w1.size).each { |i| c += 1 if w1[i] == w2[i] } c end def common3(w1, w2) c = 0 for i in (0...w1.size) do c += 1 if w1[i] == w2[i] end c end Benchmarking these with five-letter words at 10000 iterations: user system total real 1.990000 0.000000 1.990000 ( 1.983015) 1.920000 0.000000 1.920000 ( 1.922185) 1.570000 0.000000 1.570000 ( 1.570696) on my humble little Pentium 200. Speed isn't actually too vital here -- the method gets called only once per turn. Still, for language-learning purposes I'm interested in any thoughts on other ways to go about it and/or refine the ones I've already got. One particular question: Is (0...w1.size) really the best way to iterate through that range? each[[_with]_index] seem to require splitting the string on //. Is there any way to combine splitting by character and iterating? David -- David Alan Black home: dblack / candle.superlink.net work: blackdav / shu.edu Web: http://pirate.shu.edu/~blackdav