Created
May 17, 2018 02:20
-
-
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.
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
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