Skip to content

Instantly share code, notes, and snippets.

@gabraganca
Created May 17, 2018 02:20
Show Gist options
  • Save gabraganca/7944c43a8fa9447ef22f7d084c243e84 to your computer and use it in GitHub Desktop.
Save gabraganca/7944c43a8fa9447ef22f7d084c243e84 to your computer and use it in GitHub Desktop.
Check if the word is or almost is a palindrome. To be almost a palindrome, we would need to change only one character.
def IsAlmostPalindrome(word):
"""
Check if the word is or almost is a palindrome.
To be almost a palindrome, we would need to change
only one character.
Parameters
----------
word: str
The word to be checked
Returns
-------
True if the word is a palindrome or almost. False
otherwise
"""
count = 0
for i in range(len(word)//2):
if word[i] != word[-i-1]:
count+=1
if count >1:
return False
return True
assert IsAlmostPalindrome('abccba')
assert IsAlmostPalindrome('abccbg')
assert not IsAlmostPalindrome('abccfg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment