Last active
December 17, 2015 15:09
-
-
Save ethagnawl/5630087 to your computer and use it in GitHub Desktop.
A simple palindrome matcher.
This file contains 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 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