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.
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:
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:
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: //bit.ly/3ndmjEN