Skip to content

Instantly share code, notes, and snippets.

@dmitry-ilyashevich
Forked from ka8725/gist:6242120
Last active December 21, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmitry-ilyashevich/6242883 to your computer and use it in GitHub Desktop.
Save dmitry-ilyashevich/6242883 to your computer and use it in GitHub Desktop.
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