Skip to content

Instantly share code, notes, and snippets.

@benolee
Created May 17, 2011 22:05
Show Gist options
  • Select an option

  • Save benolee/977515 to your computer and use it in GitHub Desktop.

Select an option

Save benolee/977515 to your computer and use it in GitHub Desktop.
Anagram game solver
module Anagram
class Wordlist
def load_words
@words = Hash.new{[]}
File.open("/usr/share/dict/words", "r") do |file|
while line = file.gets
word = line.chomp
@words[word.split('').sort!.join('')] += [word]
end
end
end
def find_word(str)
anagrams = []
3.upto(str.size).each do |n|
str.split(//).combination(n).map{|w| w.sort.join}.each do |perm|
anagrams << @words[perm]
end
end
anagrams.flatten.uniq
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment