Python program to check whether number formed by combining all elements of the array is palindrome
Last Updated :
25 Mar, 2023
Given an array arr[], the task is to combine all the elements in the array sequentially and check if it is a palindrome.
Examples:
Input: arr[] ={1 , 69 , 54 , 45 , 96 , 1}
Output: palindrome
Explanation: The number formed by combining all the elements is “1695445961” which is a palindrome
Input: arr[] ={2 , 73 , 95 , 59 , 96 , 2}
Output: not palindrome
Explanation: The number formed by combining all the elements is “2739559962” which is not a palindrome
Method 1: Using map() and join()
- Convert each element of the list to a string using map() function.
- Join the list using join() function.
- Check if it is a palindrome.
- If yes then print palindrome.
- If no print, not a palindrome.
Below is the implementation of the above approach:
Python3
def checkPalindrome(string):
rev = string[:: - 1 ]
if (string = = rev):
return True
else :
return False
def joinArray(lis):
lis = list ( map ( str , lis))
number = ''.join(lis)
if (checkPalindrome(number)):
return True
else :
return False
lis = [ 1 , 76 , 39 , 93 , 67 , 1 ]
if (joinArray(lis)):
print ( "Palindrome" )
else :
print ( "not Palindrome" )
|
Time Complexity: O(n)
Method 2: Using type casting and string concatenation
- Take an empty string say str.
- Traverse through the list and convert each element to string using type casting
- Add this to str using string concatenation
- Check if str is a palindrome
Below is the implementation of the above approach:
Python3
def checkPalindrome(string):
rev = string[:: - 1 ]
if (string = = rev):
return True
else :
return False
def joinArray(lis):
number = ""
for i in lis:
i = str (i)
number = number + i
if (checkPalindrome(number)):
return True
else :
return False
lis = [ 1 , 76 , 39 , 93 , 67 , 1 ]
if (joinArray(lis)):
print ( "Palindrome" )
else :
print ( "not Palindrome" )
|
Method 3 : Using str(),list(),extend(),join() and reverse() methods
Python3
lis = [ 1 , 76 , 39 , 93 , 67 , 1 ]
s = ""
for i in lis:
s + = str (i)
x = list (s)
y = []
y.extend(x)
x.reverse()
if (x = = y):
print ( "Palindrome" )
else :
print ( "not Palindrome" )
|
Method 4 : Using reduce():
Algorithm:
- Import the “reduce” function from the “functools” module.
- Create a list of integers “lst”.
- Use the “reduce” function to concatenate all the integers in the list into a single string “s”.
- Check whether the string “s” is equal to its reverse.
- If “s” is equal to its reverse, print “Palindrome”; otherwise, print “Not Palindrome”.
Python3
from functools import reduce
lst = [ 1 , 76 , 39 , 93 , 67 , 1 ]
s = reduce ( lambda x, y: str (x) + str (y), lst)
if s = = s[:: - 1 ]:
print ( "Palindrome" )
else :
print ( "Not Palindrome" )
|
Time complexity: O(n), where n is the number of integers in the list “lst”. This is because the reduce function takes O(n) time to concatenate all the integers into a single string, and checking whether the string is equal to its reverse takes O(n/2) time.
Auxiliary Space: O(n), where n is the number of integers in the list “lst”. This is because the reduce function creates a new string of length n by concatenating all the integers in the list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...