[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[QUIZ][SOLUTION] Scrabble Stems (#12

Martin DeMello

12/19/2004 8:41:00 PM

Here's my solution. I used the sowpods wordlist available here:
http://www.isc.ro/commands/... (preprocessed to be all-lowercase,
though it doesn't really matter as long as the case is consistent).

It takes entirely too long to run (12 seconds on my P4/3GHz); I'll poke
at it some more if I get the time and see what's slowing it down.

--------------------------------------------------------------------------
$stems = {}
$seen = {}
$cutoff = ARGV[0].to_i

IO.foreach("sowpods.txt") {|word|
next if word.length != 8
word.chomp!
letters = word.split(//).sort
alpha = letters.join
next if $seen[alpha]
$seen[alpha] = true
remove = letters.uniq
remove.each {|i|
stem = alpha.sub(i, '')
$stems[stem] ||= {}
$stems[stem][i] = true
}
}

$stem_length = {}
$stems.each {|k,v| $stems[k] = v.keys.sort}
$stems.reject! {|k,v|
n = v.length
$stem_length[k] = n
n < $cutoff
}

results = $stems.keys.sort_by {|i| -$stem_length[i]}
results.each {|s| p [s, $stem_length[s], $stems[s].join]}
2 Answers

Andrew Johnson

12/19/2004 9:28:00 PM

0

Here's mine ... doesn't seem to fair too badly. Using the YAWL list
(264,061 words), it finds 25 bingo-stems that combine with n >= 20 letters.
(about 16 seconds on a 666MHz).

regards,
andrew

#!/usr/bin/ruby -w

n = ARGV[0].to_i
wfile = ARGV[1]

w7sigs = {}
w6sigs = {}

File.open(wfile,'r') do |f|
f.each do |line|
next unless line.size == 8
line.chomp!
line.downcase!
sig = line.unpack('aaaaaaa').sort.join("")
next if w7sigs[sig]
w7sigs[sig] = 1
end
end

w7sigs.each_key do |k|
7.times do |i|
ns = k.dup
ll = ns.slice!(i,1)
w6sigs[ns] ||= []
w6sigs[ns] << ll
end
end

w6sigs.each {|k,v| w6sigs[k].uniq!}
w6sigs.reject!{|k,v|v.size < n}
w6sigs.sort_by{|a|a[1].size}.reverse.each do |it|
puts "#{it[0]} #{it[1].size}"
end
__END__

Dave Burt

12/20/2004 9:24:00 AM

0

Hi,

Nothing special here: http://www.dave.burt.id.au/ruby/scrabbl...

It's quite ridiculous, actually. It makes a tree of about 45MB out of a
2.6MB input file, so it can tell you a word each stem can make with each
letter you can add to it.

Takes a minute for words above 20 on my machine (1.8GHz) using the
dictionary I mentioned. YAWL or something.

Just thought I might weigh in.

Season's greetings,
Dave