Saturday, December 28, 2024
Google search engine
HomeLanguagesPython program to check if number is palindrome (one-liner)

Python program to check if number is palindrome (one-liner)

Sometimes, we have an application of checking a number is palindrome or not and it’s quite common while day-day programming or competitive programming, it’s easy to reverse a number and check it but sometimes for readability and reducing lines of code, we require to perform this in one-liner logic. Let’s discuss certain ways in which this can be achieved.

Input : test_number = 12321 Output : True Input : test_number = 1234 Output : False

Method #1 : Using math.log() + recursion + list comprehension The combination of above three functions can perform this particular task easily, the logs function extracts the number of digits which is powered by 10 to get the number for that iteration for comparison. The process is recurred to test for palindrome. 

Python3




# Python3 code to demonstrate
# checking a number is palindrome
# using math.log() + recursion + list comprehension
import math
  
# the recursive function to reverse
def rev(num):
    return int(num != 0) and ((num % 10) * \
             (10**int(math.log(num, 10))) + \
                          rev(num // 10))
 
# initializing number
test_number = 9669669
 
# printing the original number
print ("The original number is : " + str(test_number))
 
# using math.log() + recursion + list comprehension
# for checking a number is palindrome
res = test_number == rev(test_number)
 
# printing result
print ("Is the number palindrome ? : " + str(res))


Output:

The original number is : 9669669
Is the number palindrome ? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

  Method #2 : Using str() + string slicing This can also be done by converting the number into a string and then reversing it using the string slicing method and comparing it, truth of which returns the answer to it. 

Python3




# Python3 code to demonstrate
# checking a number is palindrome
# using str() + string slicing
 
# initializing number
test_number = 9669669
 
# printing the original number
print ("The original number is : " + str(test_number))
 
# using str() + string slicing
# for checking a number is palindrome
res = str(test_number) == str(test_number)[::-1]
 
# printing result
print ("Is the number palindrome ? : " + str(res))


Output:

The original number is : 9669669
Is the number palindrome ? : True

Time Complexity: O(n)
Auxiliary Space: O(1)

We can also read input as string and then simply check for palindrome. 

Python3




num = input("Enter a number")
if num == num[::-1]:
    print("Yes its a palindrome")
else:
    print("No, its not a palindrome")


  Method #3 : Using all() and zip()

Python3




#Using the all function and a generator expression to check if a number is a palindrome
print(all(a == b for a, b in zip(str(12321), reversed(str(12321)))))  # prints True
print(all(a == b for a, b in zip(str(1234), reversed(str(1234)))))  # prints False
#This code is contributed by Edula Vinay Kumar Reddy


Output

True
False

This code uses the all function and a generator expression to check if a number is a palindrome.

The zip function takes two iterables (in this case, the string representation of the number and the reversed string representation of the number) and returns an iterator of tuples, where each tuple contains one element from each iterable. For example, zip(str(12321), reversed(str(12321))) would return an iterator of the following tuples:

(‘1’, ‘1’)
(‘2’, ‘2’)
(‘3’, ‘3’)
(‘2’, ‘2’)
(‘1’, ‘1’)

The generator expression (a == b for a, b in zip(str(12321), reversed(str(12321)))) generates a generator that returns True if the elements a and b are equal, and False otherwise. In this case, it would generate the following values:

True
True
True
True
True

The all function returns True if all of the elements in the generator are True, and False otherwise. In this case, it would return True because all of the elements in the generator are True.

Time complexity: O(n), where n is the length of the number. This is because the zip function and the generator expression both have a time complexity of O(n).

Space complexity: O(1), because the code only uses a fixed amount of space regardless of the length of the number.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments