Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 5, 2015 22:19
Show Gist options
  • Save mayfer/019e8e11e1bd51eb9958 to your computer and use it in GitHub Desktop.
Save mayfer/019e8e11e1bd51eb9958 to your computer and use it in GitHub Desktop.
Find longest palindromes
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