Answer to Question #187108 in Python for mani

Question #187108

Closest Palindrome Number

Given a string N, representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.

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

Input

The input will be a single line containing an integer.

Output

The output should be a single line containing the closest palindrome number to the given integer.

Explanation

For example, if the given integer is 19, the palindrome number greater than 19 is 22 and the palindrome number less than 19 is 11. As 22 is closest to the number 19. So the output should be 22.

For example, if the given integer is 15, the palindrome number greater than 15 is 22 and the palindrome number less than 15 is 11. As 11 is closest to the number 19. So the output should be 11.


Sample Input 1

19


Sample Output 1

22


Sample Input 2

15


Sample Output 2

11




1
Expert's answer
2021-04-30T23:13:53-0400
def main():
    number=int(input())
    counter1=0
    counter2=0
    closestIntegerPalindrome1=number
    closestIntegerPalindrome1=number
    for n in range(number,number-100,-1):
        if isPalindrome(n):
            closestIntegerPalindrome1=n
            break
        counter1+=1
    for n in range(number,number+100):
        if isPalindrome(n):
            closestIntegerPalindrome2=n
            break
        counter2+=1


    if(counter1<counter2):
        print(closestIntegerPalindrome1)
    else:
        print(closestIntegerPalindrome2)


def isPalindrome(number):
    tempNumber=number
    reverseNumber=0
    while(number>0):
        digit=number%10
        reverseNumber=reverseNumber*10+digit
        number=number//10
    if(tempNumber==reverseNumber):
        return True
    else:
        return False




main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS