Created
February 20, 2014 21:41
-
-
Save mayfer/9123824 to your computer and use it in GitHub Desktop.
Find the word with most number of anagrams
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
words = File.open('/usr/share/dict/words').read | |
words.gsub!(/\r\n?/, "") | |
hashes = {} | |
anagrams = {} | |
most = 1 | |
most_anagram = '' | |
words.each_line do |word| | |
word = word.strip.downcase | |
sorted = word.chars.sort.join('') | |
if hashes[sorted] == nil | |
hashes[sorted] = 0 | |
end | |
hashes[sorted] += 1 | |
if hashes[sorted] >= most | |
most = hashes[sorted] | |
most_anagram = sorted | |
print most, " ", word | |
puts | |
end | |
end | |
puts | |
words.each_line do |word| | |
word = word.strip.downcase | |
sorted = word.chars.sort.join('') | |
if sorted == most_anagram | |
print word | |
puts | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment