- 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 it is possible to create a polygon with given n sidess in Python
Suppose we have an array nums that contain the size of n sides. We have to check whether we can form a polygon with all of the given sides or not.
So, if the input is like nums = [3, 4, 5], then the output will be True because there are three sides and sum of any two sides is larger than the 3rd one. To solve this, we will use this property where length of one side is smaller than the sum of all other sides.
To solve this, we will follow these steps −
- sort the list nums
- if last element of nums < sum of all elements in nums except last one, then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
def solve(nums): nums.sort() if nums[-1] < sum(nums[:-1]): return True return False nums = [3, 4, 5] print (solve(nums))
Input
[3, 4, 5]
Output
True
Advertisements