Closest palindrome number in python assignment expert

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.

The input will be a single line containing an integer.

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

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

Closest palindrome Number:

Given a string N, representing an integer, return the closest integer(not including itself), which is 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 a 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.

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

Python Basic - 1: Exercise-139 with Solution

Write a Python program to find the closest palindrome number of a given integer. If there are two palindrome numbers in absolute distance return the smaller number

Sample Solution-1:

Python Code:

def test(n):
    x = n
    y = n
    while True:
        if str(x) == str(x)[::-1]:
            return x
        x -=  1
        if str(y) == str(y)[::-1]:
            return y
        y += 1
    return int(bin(n)[::-1][:-2], 2)

n = 120;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 321;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 43;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 1234;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));

Sample Output:

Original number:  120
Closest Palindrome number of the said number:  121
Original number:  321
Closest Palindrome number of the said number:  323
Original number:  43
Closest Palindrome number of the said number:  44
Original number:  1234
Closest Palindrome number of the said number:  1221

Flowchart:

Closest palindrome number in python assignment expert

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

def test(n):
     result = 0
     while n:
          if str(n-result)==str(n-result)[::-1]:
              return n-result
          elif str(n+result)==str(n+result)[::-1]:
              return n+result
          result+=1

n = 120;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 321;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 43;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));
n = 1234;
print("Original number: ", n);
print("Closest Palindrome number of the said number: ",test(n));

Sample Output:

Original number:  120
Closest Palindrome number of the said number:  121
Original number:  321
Closest Palindrome number of the said number:  323
Original number:  43
Closest Palindrome number of the said number:  44
Original number:  1234
Closest Palindrome number of the said number:  1221

Flowchart:

Closest palindrome number in python assignment expert

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to reverse the binary representation of an given integer and convert the reversed binary number into an integer.
Next: Write a Python program to convert all items in a given list to float values.

Python: Tips of the Day

You can turn a set into an immutable set

This way, you can no longer modify it:

my_set = frozenset(['a', 'b', 'c', 'd'])

my_set.add("a")

If you do that, an error will be thrown:

AttributeError: 'frozenset' object has no attribute 'add'

Ref: https://bit.ly/3ndmjEN


How do I find the nearest palindrome number in Python?

Next, to find the next palindrome, simply increment the number till your palindrome check is true. Here is how it works: $ python t.py Enter a number: 123 You entered 123, but the next palindrome is 131 $ python t.py Enter a number: 121 Congratulations!

How do I find the closest palindrome number?

If the number is a single-digit number, the closest palindrome is calculated by decrementing 1 from it. If the number is 10, 100, 1000, and so on, subtract 1 from it to get the closest palindrome.

How do you create a palindrome in Python?

Palindrome Program.
str = 'JaVaJ'.
strstr = str.casefold().
# This string is reverse..
rev = reversed(str).
if list(str) == list(rev):.
print("PALINDROME !").
print("NOT PALINDROME !").

What is palindrome integer in Python?

So is the integer is same in forward or reverse order both, then the number is palindrome. For an example suppose the number is 454, if we reverse it will be 454 again. So this is palindrome. Now if the number is -565, then the reverse will be 565-, that is not same, so this will not be palindrome.