human
I found an interesting problem on https://leetcode.com/problems/find-the-closest-palindrome/:
Given an integer n, find the closest integer (not including itself), which is a palindrome. 

The 'closest' is defined as absolute difference minimized between two integers.

Example 1:

Input: "123"
Output: "121"


Note:

The input n is a positive integer represented by string, whose length will not exceed 18.
If there is a tie, return the smaller one as answer.

I tried it in Python, but could not do it. Can you solve it?

 Give me a Python program to find the closest integer (not including itself), which is a palindrome?
assistant
#Here is Python program to find the closest integer (not including itself), which is a palindrome.

class Solution:
     def nearestPalindromic(self, num):
         """
         :type n: str
         :rtype: str
         """
         K = len(num)
         candidates = set([10**K + 1, 10**(K-1) - 1])
         Prefix = int(num[:(K+1)//2])
 
         for start in map(str, [Prefix-1, Prefix, Prefix+1]):
             candidates.add(start + [start, start[:-1]][K & 1][::-1])
 
         candidates.discard(num)
 
         return str(min(candidates, key=lambda x: (abs(int(x) - int(num)), int(x))))