Mark Woodward
1/27/2007 12:39:00 PM
Hi all,
I'm hoping for some critique of the code below.
It works but is slow and probably not 'the ruby way'
---------------------------------------------------------
# Anagrams
#
# Purpose: find all anagrams in /usr/share/dict/words
# for 4 letter words (length 5 because of \n)
# path to the word file
filename = "/usr/share/dict/words"
# check file exists and is readable
if File.file?(filename) && File.readable?(filename) then
dict_words = []
all_anagrams = {}
open(filename).each do |dict_word|
if dict_word.length == 5 then
dict_words << dict_word.chomp.strip
end
end
wrds = dict_words
dict_words.each do |wrd|
current_wrd = wrd.split(//).sort.join
anagrams = []
wrds.each do |w|
if wrd != w then
if not current_wrd == w.split(//).sort.join then
next
end
anagrams.push(w)
end
end
if not anagrams.empty? then
all_anagrams[wrd] = anagrams
end
end
p all_anagrams
end
---------------------------------------------------------
thanks,
--
Mark