Created
February 5, 2015 22:19
-
-
Save mayfer/019e8e11e1bd51eb9958 to your computer and use it in GitHub Desktop.
Find longest palindromes
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
def find_longest_palindromes | |
words = File.open('/usr/share/dict/words').read | |
longest_words = [] | |
words.each_line do |line| | |
word = line.strip | |
if word == word.reverse | |
if longest_words.empty? || longest_words.first.length < word.length | |
longest_words = [word] | |
elsif longest_words.first.length == word.length | |
longest_words << word | |
end | |
end | |
end | |
longest_words | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment