[QUIZ][SOLUTION] Scrabble Stems (#12)

M

Martin DeMello

Here's my solution. I used the sowpods wordlist available here:
http://www.isc.ro/commands/lists.html (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] = 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}
results.each {|s| p [s, $stem_length, $stems.join]}
 
A

Andrew Johnson

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__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top