Ad

Just converting the int into string, reversing it by ( [::-1] ) and finaly converting again to an int.

Code
Diff
  • def reverse_int(num):
        return int(str(num)[::-1])
    • def reverse_int(int_reverse: int) -> int:
    • div_mod: (int, int) = divmod(int_reverse, 10)
    • reverse_int: int = div_mod[1]
    • while div_mod[0]:
    • div_mod = divmod(div_mod[0], 10)
    • reverse_int *= 10
    • reverse_int += div_mod[1]
    • return reverse_int
    • def reverse_int(num):
    • return int(str(num)[::-1])

First checking for exceptions such as len() == 0 and len() == 1.
Then saving in sol wether is a palindromic by checking all posibilities in it.
Later checking for exceptions such as len() == 0.
Finally showing the lenth of the largest solution.

Code
Diff
  • def length_longest_palindrome(string):
        n = len(string)
        if n in [0,1]: return n
        s = range(n)
        sol = [string[i:j+1] for i in s[:-1] for j in s[1:] if string[i:j+1] == string[i:j+1][::-1]]
        if len(sol) == 0: return 0
        return max([len(i) for i in sol])
    • def length_longest_palindrome(string):
    • n = len(string)
    • if n == 0:
    • return 0
    • maxlen = 1
    • table = [[False for i in range(n)]for j in range(n)]
    • for i in range(n):
    • table[i][i] = True
    • start = 0
    • for i in range(n-1):
    • if string[i] == string[i+1]:
    • table[i][i+1] = True
    • start = i
    • maxlen = 2
    • for k in range(3,n+1):
    • for i in range(n-k+1):
    • j = i + k - 1
    • if table[i+1][j-1] and string[i] == string[j]:
    • table[i][j] = True
    • if k > maxlen:
    • start = i
    • maxlen = k
    • return maxlen
    • n = len(string)
    • if n in [0,1]: return n
    • s = range(n)
    • sol = [string[i:j+1] for i in s[:-1] for j in s[1:] if string[i:j+1] == string[i:j+1][::-1]]
    • if len(sol) == 0: return 0
    • return max([len(i) for i in sol])