Saturday, August 31, 2013

Coderbyte: Palindrome (Ruby)

PROBLEM

Have the function Palindrome(str) take the str parameter being passed and return the string true if the parameter is a palindrome, (the string is the same forward as it is backward) otherwise return the string false. For example: "racecar" is also "racecar" backwards. Punctuation and numbers will not be part of the string.

SOLUTION

def Palindrome(str)
    str.gsub(/[^0-9A-Za-z]/, '') == str.gsub(/[^0-9A-Za-z]/, '').reverse ? true : false
end
 
print Palindrome(STDIN.gets)