Skip to content

Instantly share code, notes, and snippets.

@ewhitebloom
Created November 1, 2015 17:37
Show Gist options
  • Save ewhitebloom/027f2bcf92ba3a4b164d to your computer and use it in GitHub Desktop.
Save ewhitebloom/027f2bcf92ba3a4b164d to your computer and use it in GitHub Desktop.
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