Created
May 17, 2011 22:05
-
-
Save benolee/977515 to your computer and use it in GitHub Desktop.
Anagram game solver
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
| 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