Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. # Write a method that takes a string and returns true if it is a
  2. # palindrome. A palindrome is a string that is the same whether written
  3. # backward or forward. Assume that there are no spaces; only lowercase
  4. # letters will be given.
  5. #
  6. # Difficulty: easy.
  7.  
  8. def palindrome?(string)
  9. reverse=""
  10. i=0
  11. while i<string.length
  12. reverse=string[i]+reverse
  13. i+=1
  14. end
  15.  
  16. if reverse == string
  17. return true
  18. else return false
  19. end
  20. end
  21.  
  22. # These are tests to check that your code is working. After writing
  23. # your solution, they should all print true.
  24.  
  25. puts("\nTests for #palindrome?")
  26. puts("===============================================")
  27. puts('palindrome?("abc") == false: ' + (palindrome?('abc') == false).to_s)
  28. puts('palindrome?("abcba") == true: ' + (palindrome?('abcba') == true).to_s)
  29. puts('palindrome?("z") == true: ' + (palindrome?('z') == true).to_s)
  30. puts("===============================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement