![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
Check if given four integers (or sides) make rectangle in Python
Suppose we have a list of four sides, we have to check whether these four sides are forming a rectangle or not.
So, if the input is like sides = [10, 30, 30, 10], then the output will be True as there are pair of sides 10 and 30.
To solve this, we will follow these steps −
- if all values of sides are same, then
- return True
- otherwise when sides[0] is same as sides[1] and sides[2] is same as sides[3], then
- return True
- otherwise when sides[0] is same as sides[3] and sides[2] is same as sides[1], then
- return True
- otherwise when sides[0] is same as sides[2] and sides[3] is same as sides[1], then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
def solve(sides): if sides[0] == sides[1] == sides[2] == sides[3]: return True elif sides[0] == sides[1] and sides[2] == sides[3]: return True elif sides[0] == sides[3] and sides[2] == sides[1]: return True elif sides[0] == sides[2] and sides[3] == sides[1]: return True return False sides = [10, 30, 30, 10] print(solve(sides))
Input
[10, 30, 30, 10]
Output
True
Advertisements