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

Discussion in 'Ruby' started by Martin DeMello, Dec 19, 2004.

1. ### Martin DeMelloGuest

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]}

Martin DeMello, Dec 19, 2004

2. ### Andrew JohnsonGuest

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__

Andrew Johnson, Dec 19, 2004