Skip to content

Instantly share code, notes, and snippets.

@monicao
Last active December 14, 2015 17:08
Show Gist options
  • Save monicao/5119664 to your computer and use it in GitHub Desktop.
Save monicao/5119664 to your computer and use it in GitHub Desktop.
Palindrome problem
# Returns true if sentence is a palindrome.
# racecar ---> true
# hello ---> false
# aba aba ---> true
# Never odd or even ---> true
def palindrome?(sentence)
# downcase the sentence
downcased_sentence = sentence.downcase
# remove all the spaces in the sentence
sentence_without_spaces = downcased_sentence.delete(' ')
puts "Sentence: #{sentence_without_spaces}"
# reverse the word
reversed = sentence_without_spaces.reverse
# compare the reversed word with the non-reversed word.
puts "Reversed: #{reversed}"
if sentence_without_spaces == reversed
return true
end
return false
# if they are equal return true
end
puts palindrome?("racecar")
puts palindrome?("hello")
puts palindrome?("Never odd or even")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment