
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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
- Related Articles
- Program to check whether given tree is symmetric tree or not in Python
- Program to check whether list of points form a straight line or not in Python
- Program to check whether given list is in valid state or not in Python
- C Program To Check whether Matrix is Skew Symmetric or not?
- Identify the shapes given below. Check whether they are symmetric or not. Draw the line of symmetry as well."\n
- Check whether the point (x, y) lies on a given line in Python
- Python program to check whether a list is empty or not?
- Program to check whether given graph is bipartite or not in Python
- Program to check whether given password meets criteria or not in Python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether given words are maintaining given pattern or not in C++
- Python program to check whether a given string is Heterogram or not
- Program to check whether given matrix is Toeplitz Matrix or not in Python
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether elements frequencies are even or not in Python

Advertisements