Python: Closest Palindrome number of a given integer
Closest Palindrome Number
Write a Python program to find the closest palindrome number to a given integer. If there are two palindrome numbers in absolute distance return the smaller number.
Sample Solution-1:
Python Code:
# Define a function 'test' that finds the closest palindrome number to the given integer 'n'.
def test(n):
# Initialize two variables 'x' and 'y' to the original value of 'n'.
x = n
y = n
# Loop indefinitely until a palindrome is found.
while True:
# Check if the string representation of 'x' is a palindrome.
if str(x) == str(x)[::-1]:
# If 'x' is a palindrome, return it as the closest palindrome.
return x
# Decrement 'x' by 1 for the next iteration.
x -= 1
# Check if the string representation of 'y' is a palindrome.
if str(y) == str(y)[::-1]:
# If 'y' is a palindrome, return it as the closest palindrome.
return y
# Increment 'y' by 1 for the next iteration.
y += 1
# Test case 1
n1 = 120
print("Original number: ", n1)
print("Closest Palindrome number of the said number: ", test(n1))
# Test case 2
n2 = 321
print("\nOriginal number: ", n2)
print("Closest Palindrome number of the said number: ", test(n2))
# Test case 3
n3 = 43
print("\nOriginal number: ", n3)
print("Closest Palindrome number of the said number: ", test(n3))
# Test case 4
n4 = 1234
print("\nOriginal number: ", n4)
print("Closest Palindrome number of the said number: ", test(n4))
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
Explanation:
Here is a breakdown of the above Python code:
- test Function:
- The "test()" function takes an integer 'n' as input.
- It initializes two variables 'x' and 'y' to the original value of 'n'.
- The function then enters an infinite loop, decrementing 'x' and incrementing 'y' in each iteration.
- If a palindrome is found during this process, it is returned as the closest palindrome.
Flowchart:
Sample Solution-2:
Python Code:
# Define a function 'test' that finds the closest palindrome number to the given integer 'n'.
def test(n):
# Initialize a variable 'result' to 0.
result = 0
# Loop until a palindrome is found.
while n:
# Check if the string representation of 'n - result' is a palindrome.
if str(n - result) == str(n - result)[::-1]:
# If true, return 'n - result' as the closest palindrome.
return n - result
# Check if the string representation of 'n + result' is a palindrome.
elif str(n + result) == str(n + result)[::-1]:
# If true, return 'n + result' as the closest palindrome.
return n + result
# Increment 'result' for the next iteration.
result += 1
# Test case 1
n1 = 120
print("Original number: ", n1)
print("Closest Palindrome number of the said number: ", test(n1))
# Test case 2
n2 = 321
print("\nOriginal number: ", n2)
print("Closest Palindrome number of the said number: ", test(n2))
# Test case 3
n3 = 43
print("\nOriginal number: ", n3)
print("Closest Palindrome number of the said number: ", test(n3))
# Test case 4
n4 = 1234
print("\nOriginal number: ", n4)
print("Closest Palindrome number of the said number: ", test(n4))
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
Explanation:
Here is a breakdown of the above Python code:
- test Function:
- The "test()" function takes an integer 'n' as input.
- It initializes a variable result to 0.
- The function enters a loop until a palindrome is found:
- It checks if n - result or n + result is a palindrome.
- If true, it returns the corresponding value as the closest palindrome.
Flowchart:
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics