i got a task to go over a string and check if its reversable or almost reversable (removing 1 char from the string and it becomes reversible) and i need to return True or False,
in the code below it enters the if even when the two chars are the same.
def Question3 (str):
reverse_str = str[::-1]
count_difference = 0
i = 0
while i < len(str):
if str[i] != reverse_str[i]:
count_difference += 1
str.replace(str[i],'')
reverse_str.replace(reverse_str[i],'')
i = i - 1
i += 1
if count_difference > 1:
return False
else:
return True
print(Question3('annnnba')) #suppose to be True, but it got stuck on infinite loop on the first char
New contributor
Misha Ruban is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.