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 >>>
(nums: list[float])
| 2 | |
| 3 | |
| 4 | def check_polygon(nums: list[float]) -> 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([1, 4.3, 5.2, 12.2]) |
| 18 | False |
| 19 | >>> nums = [3, 7, 13, 2] |
| 20 | >>> _ = check_polygon(nums) # Run function, do not show answer in output |
| 21 | >>> nums # Check numbers are not reordered |
| 22 | [3, 7, 13, 2] |
| 23 | >>> check_polygon([]) |
| 24 | Traceback (most recent call last): |
| 25 | ... |
| 26 | ValueError: Monogons and Digons are not polygons in the Euclidean space |
| 27 | >>> check_polygon([-2, 5, 6]) |
| 28 | Traceback (most recent call last): |
| 29 | ... |
| 30 | ValueError: All values must be greater than 0 |
| 31 | """ |
| 32 | if len(nums) < 2: |
| 33 | raise ValueError("Monogons and Digons are not polygons in the Euclidean space") |
| 34 | if any(i <= 0 for i in nums): |
| 35 | raise ValueError("All values must be greater than 0") |
| 36 | copy_nums = nums.copy() |
| 37 | copy_nums.sort() |
| 38 | return copy_nums[-1] < sum(copy_nums[:-1]) |
| 39 | |
| 40 | |
| 41 | if __name__ == "__main__": |