Syntax error Check if all elements of the array are palindrome or not in Python

Check if all elements of the array are palindrome or not in Python



Suppose we have a list of numbers nums. We have to check whether the list is palindrome or not.

So, if the input is like nums = [10, 12, 15, 12, 10], then the output will be True.

To solve this, we will follow these steps −

  • n := size of nums
  • reset is_palindrome
  • i := 0
  • while i <= quotient of (n / 2) and n is not 0, do
    • if nums[i] is not same as nums[n - i - 1], then
      • set is_palindrome
      • come out from the loop
    • i := i + 1
  • if is_palindrome is set, then
    • return False
  • otherwise,
    • return True

Let us see the following implementation to get better understanding −

Example

 Live Demo

def solve(nums):
   n = len(nums)
   is_palindrome = 0
   i = 0
   while i <= n // 2 and n != 0:
      if nums[i] != nums[n - i - 1]:
         is_palindrome = 1
         break
      i += 1
   if is_palindrome == 1:
      return False
   else:
      return True
nums = [10, 12, 15, 12, 10]
print(solve(nums))

Input

[10, 12, 15, 12, 10]

Output

True
Updated on: 2020-12-29T13:45:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements