mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[mypy] check polygon and corrections (#5419)
* Update annotations to Python 3.10 #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>
This commit is contained in:
parent
c0acfd46cb
commit
fdf095f69f
|
@ -1,7 +1,7 @@
|
||||||
from typing import List
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
def check_polygon(nums: List) -> bool:
|
def check_polygon(nums: list[float]) -> bool:
|
||||||
"""
|
"""
|
||||||
Takes list of possible side lengths and determines whether a
|
Takes list of possible side lengths and determines whether a
|
||||||
two-dimensional polygon with such side lengths can exist.
|
two-dimensional polygon with such side lengths can exist.
|
||||||
|
@ -14,15 +14,28 @@ def check_polygon(nums: List) -> bool:
|
||||||
True
|
True
|
||||||
>>> check_polygon([3, 7, 13, 2])
|
>>> check_polygon([3, 7, 13, 2])
|
||||||
False
|
False
|
||||||
|
>>> check_polygon([1, 4.3, 5.2, 12.2])
|
||||||
|
False
|
||||||
|
>>> nums = [3, 7, 13, 2]
|
||||||
|
>>> _ = check_polygon(nums) # Run function, do not show answer in output
|
||||||
|
>>> nums # Check numbers are not reordered
|
||||||
|
[3, 7, 13, 2]
|
||||||
>>> check_polygon([])
|
>>> check_polygon([])
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
...
|
...
|
||||||
ValueError: List is invalid
|
ValueError: Monogons and Digons are not polygons in the Euclidean space
|
||||||
|
>>> check_polygon([-2, 5, 6])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: All values must be greater than 0
|
||||||
"""
|
"""
|
||||||
if not nums:
|
if len(nums) < 2:
|
||||||
raise ValueError("List is invalid")
|
raise ValueError("Monogons and Digons are not polygons in the Euclidean space")
|
||||||
nums.sort()
|
if any(i <= 0 for i in nums):
|
||||||
return nums.pop() < sum(nums)
|
raise ValueError("All values must be greater than 0")
|
||||||
|
copy_nums = nums.copy()
|
||||||
|
copy_nums.sort()
|
||||||
|
return copy_nums[-1] < sum(copy_nums[:-1])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in New Issue
Block a user