python generate palindrome incrementally
For a even length palindrome you can break it into two parts: header | header.reverse()\nExample: 123 | 321, header == 123\n\nFor an odd length palindrome you c
For a even length palindrome you can break it into two parts: header | header.reverse()\nExample: 123 | 321, header == 123\n\nFor an odd length palindrome you can break it into three parts, header | mid | header.reverse()\nExample: 12 | 5 | 21, header = 12, mid = 5\n\n**Base case**\nTo generate the next palindrome, just increment the header, if it has even lenght, or the mid, if odd.\n\nExample:\n123 | 321 -> 124 | 421 -> 125 | 521\n12 | 5 | 21 -> 12 | 6 | 21 -> 12 | 7 | 21\n\n**3 corner cases**\nX is header, and length(str(X)) == length(str(X+1))\n* X |9| X   ->   X+1 |0| X+1\n* 9 |9| 9   ->   10 | 01     (odd length to even)\n* 99|99    ->   10 |0| 01  (even to odd)\n\n\n\n```\ndef is_prime(num):\n    n=int(math.sqrt(num))+1\n    for i in range(2,n):\n        if num%i==0:\n            return False\n    return True\n\ndef get_next_pali(header, mid=None):\n    n=\'\'\n    ret=\'\'\n    hstr=str(header)\n    rhstr=hstr[::-1]\n    h1str=str(header+1)\n    if mid != None:\n        # odd length\n        if mid==9 and len(hstr)!=len(h1str):\n            # 9|9|9 case, get_next_pali(9, 9), return 10|01\n            # trun into even length\n            return get_next_pali(header+1)\n        elif mid==9:\n            # X|9|X case, get_next_pali(1, 9), return 202\n            return get_next_pali(header+1, -1)\n        # X|X|X case, get_next_pali(2, 3), return 242\n        return int(hstr+str(mid+1)+rhstr), header, mid+1\n    else:\n        # even length\n        if len(h1str)!=len(hstr):\n            # 9999 case: trun into odd length, \n            # get_next_pali(10, -1), return 10001\n            return get_next_pali(int(h1str[:len(hstr)]), -1)\n        # XX|XX \n        return int(h1str+h1str[::-1]), header+1, None\n            \nclass Solution:\n    def primePalindrome(self, N):\n        dic={1:2,2:2,3:3,4:5,5:5,6:7,7:7,8:11,9:11}\n        if N in dic:\n            return dic[N]\n        \n        s=str(N)\n        header=int(s[:len(s)//2])-1\n        if len(s) %2 ==0:\n            mid=None\n        else:\n            mid=int(s[len(s)//2])-1\n        n,header,mid=get_next_pali(header, mid)\n        # print(n)\n        while( n < N):\n            n,header,mid=get_next_pali(header, mid)\n        while( not is_prime(n)):\n            n,header,mid=get_next_pali(header, mid)\n        return n\n        \n```
[]