Page 100      All Pages  All Books
89
Answer 5. Scrabble Stems
Answer
From page 13
5
Scrabble Stems
We can solve the Scrabble stem problem without much effort using the typical algorithm trade-off. We can sacrifice memory (in some cases it’s speed, or even both) for an easy-to-code solution. There are a lot of stems, but we can generate them all and store them with around 50 MB of RAM, which is not too rare these days.
Let’s see how that turns out:
#!/usr/local/bin/ruby -w
# argument parsing
DICTIONARY = if ARGV.first == "-w" ARGV.shift ARGV.shift
else
"/usr/share/dict/words"
end
if ARGV.first =~ /\A\d+\Z/ LIMIT = ARGV.first.to_i
else
puts "Usage: #{File.basename($PROGRAM_NAME)} [-d DICTIONARY_FILE] LIMIT" exit
end
# storage for all the stems we find and the letters they combine with stems = Hash.new
# read the dictionary File.foreach(DICTIONARY)
# clean up the words
word.downcase!
word.delete!("^a-z")
do |word
# skip anything but a seven letter word next unless word.length == 7

Page 100      All Pages  All Books