-
-
Save mehdi-farsi/2729f421c0d408552cf22adab45a0ca8 to your computer and use it in GitHub Desktop.
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
class String | |
def palindrome? | |
tmp = self.downcase.gsub(/[^[[:alnum:]]]/, '') # you can also use /\W/ but it'll keep the underscores | |
tmp == tmp.reverse | |
end | |
end | |
"madam".palindrome? # => true | |
"racecar".palindrome? # => true | |
"madam, . ".palindrome? # => true | |
"02/02/2020".palindrome? # => true | |
"11/11/11 11:11".palindrome? # => true | |
"Madam, I'm Adam".palindrome? # => true | |
"A man, a plan, a canal – Panama".palindrome? # => true |
possible typo - the
/i
in the regex as you are already usingdowncase
?
@go2null Indeed. Thank you! 😉
👍
There's no need to use strip at the end, the gsub is already removing the white spaces. And speaking of white spaces, should an empty string be considered a palindrome?
There's no need to use strip at the end, the gsub is already removing the white spaces. And speaking of white spaces, should an empty string be considered a palindrome?
Well done! 👍
The strip
method call comes from my previous implementation. Thank you.
Uh.. It's a good question. I think that empty strings should be considered as palindrome. But it's subjective.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
possible typo - the
/i
in the regex as you are already usingdowncase
?