The calendar is gone.
Click here to view posts


Palindromes
I was asked to check a file for palindromes. Here is the code I wrote.
# returns an array of matching palindromes in the file
# the words are grouped in arrays
# 
# [['god','dog'],['act','cat']]
#
def find_palindromes(file)
  if !file || !File.exist?(file) || !File.readable?(file)
    raise "Trouble with the file"
  end
  words = File.open(file,"r").read
  words = words.split(/\s/)
  words.uniq!

  palindrome = Hash.new(Array.new)
  
  for word in words 
   palpalindrome_word = word,split.sort.join # you can downcase and remove chars here
   palindrome[palindrome_word] = palindrome[palindrome_word].push(word)
  end
 
  palindrome.values.reject{|words| words.size<2}
end