Created
August 15, 2013 16:12
-
-
Save ka8725/6242120 to your computer and use it in GitHub Desktop.
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
def to_h(w) | |
r = {} | |
w.each_char do |ch| | |
r[ch] ||= 0 | |
r[ch] += 1 | |
end | |
r | |
end | |
@dict_word_letters = {} | |
f = File.open('wl.txt') | |
f.each_line do |word| | |
word = word.strip | |
@dict_word_letters[word] = to_h(word) | |
end | |
def anagrams(w) | |
res = [] | |
wh = to_h(w) | |
@dict_word_letters.each do |w, h| | |
res << w if wh == h | |
end | |
res | |
end | |
puts anagrams('horse') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment