[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6204] Re: Ruby hi(gh), and pointer to Jotto program

From: David Alan Black <dblack@...>
Date: 2000-11-09 12:50:28 UTC
List: ruby-talk #6204
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


In This Thread