Skip to content

Instantly share code, notes, and snippets.

@ethagnawl
Last active December 17, 2015 15:09
Show Gist options
  • Save ethagnawl/5630087 to your computer and use it in GitHub Desktop.
Save ethagnawl/5630087 to your computer and use it in GitHub Desktop.
A simple palindrome matcher.
def palindrome?(x)
x = x.to_s
if x.length == 1
return true
elsif (x.length % 2) == 0
a = x[0..(x.length / 2 - 1)]
b = x[(x.length / 2)..(x.length)].reverse
else
x = x.split('')
a = x.shift(x.length / 2)
b = x.pop(x.length - 1).reverse
end
a == b
end
palindrome?('w') # => true
palindrome?('wow') # => true
palindrome?('woow') # => true
palindrome?('foobar') # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment