Created
October 22, 2014 19:03
-
-
Save mayfer/1afd4eb72e0c7059f54a to your computer and use it in GitHub Desktop.
Find the word with most anagrams
This file contains hidden or 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
# pat, tap, apt | |
def find_longest_anagram() | |
words = File.open('/usr/share/dict/words').read | |
anagrams = Hash.new { [] } | |
most_so_far = 0 | |
most_signature = nil | |
words.each_line do |word| | |
word = word.strip.downcase | |
signature = word.split('').sort.join('') | |
anagrams[signature] <<= word | |
anagrams[signature] = anagrams[signature] & anagrams[signature] | |
if anagrams[signature].length > most_so_far | |
most_signature = signature | |
most_so_far = anagrams[signature].length | |
end | |
end | |
return anagrams[most_signature] | |
end | |
puts find_longest_anagram.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment