Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. def is_palindrome(word):
  2. """(str) -> bool
  3.  
  4. Precondition: word.isalpha and word.islower hold True
  5. Return True iff word is palindrome
  6.  
  7. >>>is_palindrome('radar'):
  8. True
  9. >>>is_palindrome('cats'):
  10. False
  11. >>>is_palindrome('toot'):
  12. True
  13. """
  14.  
  15. i = 0
  16. while i <= (len(word))//2:
  17. j = -(i+1)
  18. if word[i] == word[j]:
  19. i = i+1
  20. else:
  21. return False
  22. return True
  23.  
  24. def is_palindromic_phrase(phrase):
  25. """
  26. (str) -> bool
  27.  
  28. Precondition: if apostrophe used ensure that the different one is to denote that the phrase is a string
  29. Return True iff phrase, regardless of case and non-alphabetic characters,
  30. is a palindrome
  31.  
  32. >>>is_palindromic_phrase("Madam, I'm Adam"):
  33. True
  34. >>>is_palindromic_phrase('Hello world!'):
  35. False
  36. >>>is_palindromic_phrase('3 nurses run'):
  37. True
  38. """
  39.  
  40. for ch in phrase:
  41. if not ch.isalpha():
  42. phrase = phrase.replace(ch, '')
  43.  
  44. phrase = phrase.lower()
  45.  
  46. return is_palindrome(phrase)
  47.  
  48. def get_odd_palindrome_at():
  49. """
  50. (str, int) -> str
  51.  
  52.  
  53.  
  54. >>>get_odd_palindrome_at('fadar',2)
  55. ada
  56. >>>get_odd_palindrome_at('statistics',2)
  57. tat
  58. >>>get_odd_palindrome_at('word',3)
  59. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement