Guest User

Untitled

a guest
May 22nd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. def IsAlmostPalindrome(word):
  2. """
  3. Check if the word is or almost is a palindrome.
  4.  
  5. To be almost a palindrome, we would need to change
  6. only one character.
  7.  
  8. Parameters
  9. ----------
  10.  
  11. word: str
  12. The word to be checked
  13.  
  14. Returns
  15. -------
  16.  
  17. True if the word is a palindrome or almost. False
  18. otherwise
  19. """
  20. count = 0
  21. for i in range(len(word)//2):
  22. if word[i] != word[-i-1]:
  23. count+=1
  24.  
  25. if count >1:
  26. return False
  27.  
  28. return True
  29.  
  30.  
  31. assert IsAlmostPalindrome('abccba')
  32. assert IsAlmostPalindrome('abccbg')
  33. assert not IsAlmostPalindrome('abccfg')
Add Comment
Please, Sign In to add comment