Why is the longest_palindrome variable updated when the isPalindrome() function evaluates to False? The if/else condition is not behaving as I would expect.
def palindromeCutting(s):
if s == "":
return ""
letters = list(s)
while len(letters) > 0:
tmp = []
longest_palindrome = []
for i in range(len(letters)):
tmp.append(letters[i])
if isPalindrome(tmp):
print("updating longest with tmp")
longest_palindrome = tmp
print("longest_palindrome:",longest_palindrome)
if len(longest_palindrome) > 1:
l = len(longest_palindrome)
letters = letters[l:]
else:
return "".join(letters)
def isPalindrome(arr):
left, right = 0, len(arr) - 1
while left <= right:
if arr[left] != arr[right]:
return False
left += 1
right -= 1
return True
The output for this when run with s = "aaacodedoc" is:
longest_palindrome: ['a']
longest_palindrome: ['a', 'a']
longest_palindrome: ['a', 'a', 'a']
longest_palindrome: ['a', 'a', 'a', 'c']
longest_palindrome: ['a', 'a', 'a', 'c', 'o']
longest_palindrome: ['a', 'a', 'a', 'c', 'o', 'd']
longest_palindrome: ['a', 'a', 'a', 'c', 'o', 'd', 'e']
longest_palindrome: ['a', 'a', 'a', 'c', 'o', 'd', 'e', 'd']
longest_palindrome: ['a', 'a', 'a', 'c', 'o', 'd', 'e', 'd', 'o']
longest_palindrome: ['a', 'a', 'a', 'c', 'o', 'd', 'e', 'd', 'o', 'c']