Skip to content

Create check_polygon.py #4605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 29, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions maths/check_polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List


def check_polygon(nums: List) -> bool:
"""
Takes list of possible sidelengths and determines whether a two-dimensional polygon with such sidelengths can exist.
Return a boolean value for the < comparison of the largest sidelength 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()