Skip to content

Commit fdf095f

Browse files
NumberPiOsogithub-actions
and
github-actions
authored
[mypy] check polygon and corrections (TheAlgorithms#5419)
* Update annotations to Python 3.10 TheAlgorithms#4052 * Add floats doctest * Copy list to avoid changing input unpredictably * Refactor code to make it readable * updating DIRECTORY.md * Improve raised ValueErrors and add doctest * Split doctest in multiples lines * Change ValueError to Monogons and Digons are not poly * Correct doctest refering number of sides Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent c0acfd4 commit fdf095f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

maths/check_polygon.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import List
1+
from __future__ import annotations
22

33

4-
def check_polygon(nums: List) -> bool:
4+
def check_polygon(nums: list[float]) -> bool:
55
"""
66
Takes list of possible side lengths and determines whether a
77
two-dimensional polygon with such side lengths can exist.
@@ -14,15 +14,28 @@ def check_polygon(nums: List) -> bool:
1414
True
1515
>>> check_polygon([3, 7, 13, 2])
1616
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]
1723
>>> check_polygon([])
1824
Traceback (most recent call last):
1925
...
20-
ValueError: List is invalid
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
2131
"""
22-
if not nums:
23-
raise ValueError("List is invalid")
24-
nums.sort()
25-
return nums.pop() < sum(nums)
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])
2639

2740

2841
if __name__ == "__main__":

0 commit comments

Comments
 (0)