All palindrome numbers in a list
Last Updated :
29 Dec, 2022
Given a list, count and print all the palindrome numbers in it. Examples:
Input: 10 121 133 155 141 252
Output: 121 141 252
Total palindrome nos. are 3
Input: 111 220 784 565 498 787 363
Output: 111 565 787 363
Total palindrome nos. are 4
1. Access an element from the list.
2. Now, in a temporary variable get its reverse value.
3. Now, compare the list element value by its reverse value, if both are same print the list element and increase the counter c by 1.
4. Carry on this procedure till list becomes empty.
5. Now, print the counter value i.e total number of palindrome numbers in given list.
Python3
def palindromeNumbers(list_a):
c = 0
for i in list_a:
t = i
rev = 0
while t > 0 :
rev = rev * 10 + t % 10
t = t / / 10
if rev = = i:
print (i)
c = c + 1
print ()
print ( "Total palindrome nos. are" , c)
print ()
def main():
list_a = [ 10 , 121 , 133 , 155 , 141 , 252 ]
palindromeNumbers(list_a)
list_b = [ 111 , 220 , 784 , 565 , 498 , 787 , 363 ]
palindromeNumbers(list_b)
if __name__ = = "__main__" :
main()
|
Output
121
141
252
Total palindrome nos. are 3
111
565
787
363
Total palindrome nos. are 4
Method : Using str() and slicing
Python3
def palindromeNumbers(list_a):
c = 0
for i in list_a:
if str (i) = = str (i)[:: - 1 ]:
print (i)
c + = 1
print ( "Total palindrome nos. are" , c)
list_a = [ 10 , 121 , 133 , 155 , 141 , 252 ]
palindromeNumbers(list_a)
|
Output
121
141
252
Total palindrome nos. are 3
Method : Using recursion
One approach we can use is recursion to check for palindrome numbers.
Here is an example of how this can be implemented:
Python3
def is_palindrome(num):
if len ( str (num)) = = 1 :
return True
if str (num)[ 0 ] = = str (num)[ - 1 ]:
return is_palindrome( str (num)[ 1 : - 1 ])
return False
def palindromeNumbers(list_a):
c = 0
for i in list_a:
if is_palindrome(i):
print (i)
c + = 1
print ( "Total palindrome nos. are" , c)
list_a = [ 10 , 121 , 133 , 155 , 141 , 252 ]
palindromeNumbers(list_a)
|
Output
121
141
252
Total palindrome nos. are 3
In this implementation, the is_palindrome function uses recursion to check if a given number is a palindrome. It does this by checking the first and last digits of the number, and then calling itself with the number with the first and last digits removed. If the length of the number is 1, it returns True, since a single digit number is always a palindrome. If the first and last digits do not match, it returns False.
The palindromeNumbers function then iterates through the list and checks if each number is a palindrome using the is_palindrome function. If it is, it prints the number and increments the counter. Finally, it prints the total number of palindrome numbers in the list.
The time complexity of the above code is O(n * m), where n is the length of the input list and m is the average length of the numbers in the list. This is because the palindromeNumbers function iterates through the entire list, and for each number it calls the is_palindrome function, which takes O(m) time to check if the number is a palindrome.
The auxiliary space complexity of the above code is O(m), as the is_palindrome function uses a recursive call stack that takes up O(m) space, where m is the length of the number being checked. This is because the maximum depth of the recursive call stack is equal to the length of the number.
Share your thoughts in the comments
Please Login to comment...