Skip to content

Instantly share code, notes, and snippets.

@mmcdaris
Last active April 20, 2017 01:02
Show Gist options
  • Save mmcdaris/e9c77391710a4c22df2217dd1525a16c to your computer and use it in GitHub Desktop.
Save mmcdaris/e9c77391710a4c22df2217dd1525a16c to your computer and use it in GitHub Desktop.
lil anagram finder
# 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