![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check whether given list of blocks are symmetric over x = y line or not in python
Suppose we have a list of numbers called nums. And it is representing the height of square blocks, we have to check whether the shape is symmetric over the y = x line or not.
So, if the input is like nums = [7, 5, 3, 2, 2, 1, 1], then the output will be True
To solve this, we will follow these steps:
- i := 0
- j := size of nums - 1
- while i <= j, do
- h := nums[j]
- while i < h, do
- if nums[i] is not same as (j + 1), then
- return False
- i := i + 1
- if nums[i] is not same as (j + 1), then
- j := j - 1
- return True
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, nums): i = 0 j = len(nums) - 1 while i <= j: h = nums[j] while i < h: if nums[i] != j + 1: return False i += 1 j -= 1 return True ob = Solution() nums = [7, 5, 3, 2, 2, 1, 1] print(ob.solve(nums))
Input
[7, 5, 3, 2, 2, 1, 1]
Output
True
Advertisements