Alright I'd have to say im amazed at the speed....now my question
remains how does it all work lol?
#############
words = Hash.new([])
File.open("dict1.txt", "r") do |file| #opens the dictionary file
while line = file.gets #sets line equal to all the data in
the file
word = line.chomp #knocks the \n's off the words
words[word.split('').sort!.join('')] += [word]
#this is the one im hazy about
#it splits every letter in the file, sorts it alphabetically, joins them
all #together to form one long continuous line of letters, and combines
that to the #words pulled from the dictionary?
end
end
File.open("word_hash", "w") do |file| #creates a new file named
word_hash
Marshal.dump(words, file) #writes the output from the words hash to
the file?
end
################
words = nil #makes the words hash equal to nothing
File.open("word_hash", "r") do |file| #loads the data
words = Marshal.load(file)
end
while true
print "Enter word: "
anagram = gets.chomp
sorted_anagram = anagram.split('').sort!.join('') #sorts it the same
way as #the words hash?
p words[sorted_anagram]
end
#####################
I think the part im confused about is how the sorting actually works in
terms of not so much 'how it works' but 'why it works'.
Sorry to keep this running lol but I'd rather walk away knowing the code
rather than saying 'screw it, it works, im happy' if u know what i mean.
Thanks a ton!
--
Posted via http://www.ruby-forum.com/.