Last active
December 14, 2015 17:08
-
-
Save monicao/5119664 to your computer and use it in GitHub Desktop.
Palindrome problem
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
# 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