Skip to content

Commit b9f1815

Browse files
sarkarghyapoyea
andauthored
Create check_polygon.py (TheAlgorithms#4605)
* Create check_polygon.py * Update check_polygon.py * Update maths/check_polygon.py * Update check_polygon.py * Update check_polygon.py Co-authored-by: John Law <[email protected]>
1 parent 02bc4bf commit b9f1815

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

maths/check_polygon.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
def check_polygon(nums: List) -> bool:
5+
"""
6+
Takes list of possible side lengths and determines whether a
7+
two-dimensional polygon with such side lengths can exist.
8+
9+
Returns a boolean value for the < comparison
10+
of the largest side length with sum of the rest.
11+
Wiki: https://en.wikipedia.org/wiki/Triangle_inequality
12+
13+
>>> check_polygon([6, 10, 5])
14+
True
15+
>>> check_polygon([3, 7, 13, 2])
16+
False
17+
>>> check_polygon([])
18+
Traceback (most recent call last):
19+
...
20+
ValueError: List is invalid
21+
"""
22+
if not nums:
23+
raise ValueError("List is invalid")
24+
nums.sort()
25+
return nums.pop() < sum(nums)
26+
27+
28+
if __name__ == "__main__":
29+
import doctest
30+
31+
doctest.testmod()

0 commit comments

Comments
 (0)