Created
December 12, 2008 00:06
-
-
Save dagbrown/34956 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnagramDict | |
begin | |
@@anagramdict = Marshal.load "anagramdict.marshal" | |
rescue | |
@@anagramdict ||= Hash.new | |
File.read("/usr/share/dict/word.list").split(/\n/).each do |word| | |
sortedword=word.split('').sort.join | |
@@anagramdict[sortedword] ||= [] | |
@@anagramdict[sortedword] << word | |
end | |
File.open("anagramdict.marshal","w") do |marshalfh| | |
marshalfh.write Marshal.dump(@@anagramdict) | |
end | |
end | |
def self.[](word) | |
@@anagramdict[word.split('').sort.join] | |
end | |
end | |
p AnagramDict["there"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment