From: Brian Adkins Date: 2007-10-20T09:40:03+09:00 Subject: Re: combinations listing Program 1: create the hash and dump to disk for future use words = Hash.new([]) File.open("dictionary.txt", "r") do |file| while line = file.gets word = line.chomp words[word.split('').sort!.join('')] += [word] end end File.open("word_hash", "w") do |file| Marshal.dump(words, file) end Program 2: load the hash from disk to solve anagrams words = nil File.open("word_hash", "r") do |file| words = Marshal.load(file) end while true print "Enter word: " anagram = gets.chomp sorted_anagram = anagram.split('').sort!.join('') p words[sorted_anagram] end --- This is pretty darn fast, so unless your dictionary is huge, I'd probably skip the step of partitioning the dictionary by word size. I used a 70K word dictionary and the dumped hash was 1.5MB. Since strings are mutable in Ruby, I tried sorting the string using a quicksort algorithm instead of "split, sort, join", but since the former was in Ruby and the latter mostly in C, the latter is *much* faster. This speed disparity encourages taking an indirect route at times. Maybe some C extension guru can supply an example of extending the String class with a sort! method :) Brian Adkins