-1

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']
Zissou
  • 21
  • 3
  • You could replace the definition of isPalindrom by this much shorter one : `def isPalindrom(arr): return arr == arr[::-1]` (returns array == reverse of array) – astroide Aug 28 '21 at 15:08
  • Thanks, but I still can't see why the variable is updated when the condition evaluates to False. Do you have any idea why that is? – Zissou Aug 28 '21 at 15:14
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 30 '21 at 13:02

2 Answers2

1

you are referencing tmp with longest_palindrome. So they both point to the same object.

After 3rd iteration tmp object is still growing so the longest_palindrome does also.

longest_palindrome = tmp

You can try by copying / creating new object with the same content:

longest_palindrome = tmp[:]

poleszcz
  • 145
  • 9
1

The problem is that you are setting longest_palindrom to tmp. From that point on, tmp and longest_palindrom are pointing to the same object. Whenever tmp is modified, the modification is reflected on longest_palindrom, and vice-versa.

To avoid that, you could instead set longest_palindrom to a copy of tmp, replacing the line

longest_palindrom = tmp

by

longest_palindrom = tmp[:]

which sets longest_palindrom to a new list containing the elements of tmp.

If you want a more in-depth explanation of list aliasing, the answers to this question explain it very well.

astroide
  • 807
  • 4
  • 16