
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If All Elements of the Array Are Palindrome 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 nums[i] is not same as nums[n - i - 1], then
- if is_palindrome is set, then
- return False
- otherwise,
- return True
Let us see the following implementation to get better understanding −
Example
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
Advertisements