[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [QUIZ][SOLUTION] Word Blender (#108

Daniel Finnie

1/10/2007 2:17:00 AM

Hi Ken,

Ken Bloom wrote:
> #load dictionary
> matcher=Set.new
> allwords=Array.new

> thiswordmatcher=Set.new

I'm interested in why you used a set for matcher and an array for
allwords. Was this a kindof random decision (many of mine are!) or is
there something that allwords needed to be able to do and so is an array
or vice versa?

Thanks,
Dan

3 Answers

Ken Bloom

1/10/2007 4:06:00 AM

0

On Wed, 10 Jan 2007 11:17:24 +0900, Daniel Finnie wrote:

> Hi Ken,
>
> Ken Bloom wrote:
>> #load dictionary
>> matcher=Set.new
>> allwords=Array.new
>
>> thiswordmatcher=Set.new
>
> I'm interested in why you used a set for matcher and an array for
> allwords. Was this a kindof random decision (many of mine are!) or is
> there something that allwords needed to be able to do and so is an array
> or vice versa?

allwords needs array indexing to pick a random word from the array.
The sets, however, need duplicate removal and/or need include? to operate
in O(1).

--Ken

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Daniel Finnie

1/10/2007 8:19:00 PM

0

That makes sense :).

On a side note, I did make some methods for getting a rand value from
any enumerable (they can be integrated as instance methods):

def rand_value_each(enum)
index = rand(enum.size)
curr = 0
enum.each {|x| return [x].flatten.last if curr == index; curr += 1 }
end

def rand_value_to_a(enum)
[rand_value_ary(enum.to_a)].flatten.last
end

def rand_value_ary(ary)
ary[rand(ary.size)]
end

Dan

Ken Bloom wrote:
> On Wed, 10 Jan 2007 11:17:24 +0900, Daniel Finnie wrote:
>
>> Hi Ken,
>>
>> Ken Bloom wrote:
>>> #load dictionary
>>> matcher=Set.new
>>> allwords=Array.new
>>> thiswordmatcher=Set.new
>> I'm interested in why you used a set for matcher and an array for
>> allwords. Was this a kindof random decision (many of mine are!) or is
>> there something that allwords needed to be able to do and so is an array
>> or vice versa?
>
> allwords needs array indexing to pick a random word from the array.
> The sets, however, need duplicate removal and/or need include? to operate
> in O(1).
>
> --Ken
>

James Gray

1/10/2007 8:24:00 PM

0

On Jan 10, 2007, at 2:18 PM, Daniel Finnie wrote:

> On a side note, I did make some methods for getting a rand value
> from any enumerable (they can be integrated as instance methods):
>
> def rand_value_each(enum)
> index = rand(enum.size)

Enumerable doesn't have a size() method.

> curr = 0
> enum.each {|x| return [x].flatten.last if curr == index; curr += 1 }
> end

James Edward Gray II