Last active
April 20, 2017 01:02
-
-
Save mmcdaris/e9c77391710a4c22df2217dd1525a16c to your computer and use it in GitHub Desktop.
lil anagram finder
This file contains 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
# Anagram: a word, phrase, or name formed by rearranging the letters of another. | |
# Usage: Anagrams.new.find("anemic") | |
# ["anemic", "iceman", "cinema"] | |
class Anagrams | |
def initialize | |
@words = {} | |
File.open("/usr/share/dict/words") do |file| | |
file.each do |line| | |
@words[line.strip] = true | |
end | |
end; nil | |
end | |
def find(word) | |
letters = word.split('') | |
possible_words = letters.permutation.to_a.map(&:join) | |
possible_words.select {|w| @words.include?(w) }.sort | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment