From b9f18152b74e7a1b0b60c6c1781580e6228f4ba4 Mon Sep 17 00:00:00 2001 From: "Arghya Sarkar (ASRA)" <67339217+sarkarghya@users.noreply.github.com> Date: Wed, 29 Sep 2021 22:19:42 +0530 Subject: [PATCH] Create check_polygon.py (#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 --- maths/check_polygon.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maths/check_polygon.py diff --git a/maths/check_polygon.py b/maths/check_polygon.py new file mode 100644 index 000000000..0e7711973 --- /dev/null +++ b/maths/check_polygon.py @@ -0,0 +1,31 @@ +from typing import List + + +def check_polygon(nums: List) -> bool: + """ + Takes list of possible side lengths and determines whether a + two-dimensional polygon with such side lengths can exist. + + Returns a boolean value for the < comparison + of the largest side length with sum of the rest. + Wiki: https://en.wikipedia.org/wiki/Triangle_inequality + + >>> check_polygon([6, 10, 5]) + True + >>> check_polygon([3, 7, 13, 2]) + False + >>> check_polygon([]) + Traceback (most recent call last): + ... + ValueError: List is invalid + """ + if not nums: + raise ValueError("List is invalid") + nums.sort() + return nums.pop() < sum(nums) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()