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])
  • 1
    def reverse_int(int_reverse: int) -> int:
    
    2
        div_mod: (int, int) = divmod(int_reverse, 10)
    
    3
        reverse_int: int = div_mod[1]
    
    4
        while div_mod[0]:
    
    5
            div_mod = divmod(div_mod[0], 10)
    
    6
            reverse_int *= 10
    
    7
            reverse_int += div_mod[1]
    
    8
        return reverse_int
    
    1+
    def reverse_int(num):
    
    2+
        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])
  • 11
    def length_longest_palindrome(string):
    
    2
        n = len(string) 
    
    3
        if n == 0:
    
    4
            return 0 
    
    5
        
    
    6
        maxlen = 1
    
    7
        table = [[False for i in range(n)]for j in range(n)] 
    
    8
        for i in range(n):
    
    9
            table[i][i] = True
    
    10
            
    
    11
        start = 0 
    
    12
        for i in range(n-1):
    
    13
            if string[i] == string[i+1]:
    
    14
                table[i][i+1] = True 
    
    15
                start = i 
    
    16
                maxlen = 2 
    
    17
                
    
    18
        for k in range(3,n+1):
    
    19
            for i in range(n-k+1):
    
    20
                j = i + k - 1 
    
    21
                if table[i+1][j-1] and string[i] == string[j]:
    
    22
                    table[i][j] = True
    
    23
                    
    
    24
                    if k > maxlen:
    
    25
                        start = i 
    
    26
                        maxlen = k 
    
    27
        return maxlen 
    
    2+
        n = len(string)
    
    3+
        if n in [0,1]: return n
    
    4+
        s = range(n)
    
    5+
        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]]
    
    6+
        if len(sol) == 0: return 0
    
    7+
        return max([len(i) for i in sol])