Created
November 1, 2015 17:37
-
-
Save ewhitebloom/027f2bcf92ba3a4b164d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 isPalindrome(word) | |
word == word.reverse | |
end | |
def isPalindrome2(word) | |
i = 0 | |
lastIndex = word.length - 1 | |
while i != lastIndex - i | |
if word[i] == word[lastIndex - i] | |
i += 1 | |
else | |
return false | |
end | |
end | |
true | |
end | |
def isPalindrome3(word) | |
chars = word.split('') | |
if chars.size == 1 || chars.size == 2 | |
chars.first == chars.last | |
elsif chars.first != chars.last | |
false | |
else | |
chars.shift && chars.pop | |
isPalindrome3(chars.join) | |
end | |
end | |
puts isPalindrome('abcba') | |
puts isPalindrome2('abcba') | |
puts isPalindrome3('abcba') | |
puts isPalindrome('m') | |
puts isPalindrome2('m') | |
puts isPalindrome3('m') | |
puts isPalindrome('abcd') | |
puts isPalindrome2('abcd') | |
puts isPalindrome3('abcd') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment