-
-
Save dmitry-ilyashevich/6242883 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_i(w) | |
r = 0 | |
w.each_char { |ch| r = r | (1 << ch.ord - 96) } | |
r | |
end | |
@dict_word_letters = {} | |
f = File.open('wl.txt') | |
f.each_line do |word| | |
word = word.strip | |
@dict_word_letters[word] = to_i(word) | |
end | |
def anagrams(word) | |
res = [] | |
wh = to_i(word) | |
@dict_word_letters.each do |w, h| | |
res << w if wh == h && w.length == word.length && w.chars.sort == word.chars.sort | |
end | |
res | |
end | |
puts anagrams('horse') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment