From 9989344773a4a2312dc800c4f2e99eb494a33f86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:44:48 -0500 Subject: [PATCH 1/9] Update annotations to Python 3.10 #4052 --- maths/check_polygon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 0e771197331f..4faa3cef3b48 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -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 two-dimensional polygon with such side lengths can exist. From 5c45949a2fea1f8b1e6a44b542a86dcd92d4ad64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:46:14 -0500 Subject: [PATCH 2/9] Add floats doctest --- maths/check_polygon.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 4faa3cef3b48..a1985ddc51f7 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -14,6 +14,8 @@ def check_polygon(nums: list[float]) -> bool: True >>> check_polygon([3, 7, 13, 2]) False + >>> check_polygon([1, 4.3, 5.2, 12.2]) + False >>> check_polygon([]) Traceback (most recent call last): ... From a0fa91018ccc5d6c9509688456ca899c472f0d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:53:05 -0500 Subject: [PATCH 3/9] Copy list to avoid changing input unpredictably --- maths/check_polygon.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index a1985ddc51f7..6d54052c3f65 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -16,6 +16,9 @@ def check_polygon(nums: list[float]) -> bool: False >>> check_polygon([1, 4.3, 5.2, 12.2]) False + >>> nums = [3, 7, 13, 2];check_polygon(nums); nums + False + [3, 7, 13, 2] >>> check_polygon([]) Traceback (most recent call last): ... @@ -23,8 +26,9 @@ def check_polygon(nums: list[float]) -> bool: """ if not nums: raise ValueError("List is invalid") - nums.sort() - return nums.pop() < sum(nums) + copy_nums = nums.copy() + copy_nums.sort() + return copy_nums.pop() < sum(copy_nums) if __name__ == "__main__": From 92de316f8961c6967fa17ca5b45c7aeabdea786a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 11:54:02 -0500 Subject: [PATCH 4/9] Refactor code to make it readable --- maths/check_polygon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 6d54052c3f65..27899ccc125c 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -28,7 +28,7 @@ def check_polygon(nums: list[float]) -> bool: raise ValueError("List is invalid") copy_nums = nums.copy() copy_nums.sort() - return copy_nums.pop() < sum(copy_nums) + return copy_nums[-1] < sum(copy_nums[:-1]) if __name__ == "__main__": From 9187547e5473d863f651834ab2acfcb5d4157b2f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 18 Oct 2021 16:54:30 +0000 Subject: [PATCH 5/9] updating DIRECTORY.md --- DIRECTORY.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index c197dd88032e..d2f458638a35 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -513,9 +513,10 @@ * [Runge Kutta](https://github.com/TheAlgorithms/Python/blob/master/maths/runge_kutta.py) * [Segmented Sieve](https://github.com/TheAlgorithms/Python/blob/master/maths/segmented_sieve.py) * Series - * [Arithmetic Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/series/arithmetic_mean.py) - * [Geometric Mean](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_mean.py) + * [Arithmetic](https://github.com/TheAlgorithms/Python/blob/master/maths/series/arithmetic.py) + * [Geometric](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric.py) * [Geometric Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/geometric_series.py) + * [Harmonic](https://github.com/TheAlgorithms/Python/blob/master/maths/series/harmonic.py) * [Harmonic Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/harmonic_series.py) * [P Series](https://github.com/TheAlgorithms/Python/blob/master/maths/series/p_series.py) * [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/sieve_of_eratosthenes.py) @@ -933,6 +934,7 @@ * [Indian Phone Validator](https://github.com/TheAlgorithms/Python/blob/master/strings/indian_phone_validator.py) * [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py) * [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py) + * [Join](https://github.com/TheAlgorithms/Python/blob/master/strings/join.py) * [Knuth Morris Pratt](https://github.com/TheAlgorithms/Python/blob/master/strings/knuth_morris_pratt.py) * [Levenshtein Distance](https://github.com/TheAlgorithms/Python/blob/master/strings/levenshtein_distance.py) * [Lower](https://github.com/TheAlgorithms/Python/blob/master/strings/lower.py) @@ -967,6 +969,7 @@ * [Fetch Jobs](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_jobs.py) * [Get Imdb Top 250 Movies Csv](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdb_top_250_movies_csv.py) * [Get Imdbtop](https://github.com/TheAlgorithms/Python/blob/master/web_programming/get_imdbtop.py) + * [Giphy](https://github.com/TheAlgorithms/Python/blob/master/web_programming/giphy.py) * [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py) * [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py) * [Instagram Video](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_video.py) From 7ae3a6c84566f2bd7014c0c6a445823746db25ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Mon, 18 Oct 2021 12:03:43 -0500 Subject: [PATCH 6/9] Improve raised ValueErrors and add doctest --- maths/check_polygon.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 27899ccc125c..0283803bad51 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -22,10 +22,16 @@ def check_polygon(nums: list[float]) -> bool: >>> check_polygon([]) Traceback (most recent call last): ... - ValueError: List is invalid + ValueError: List must have at least two values + >>> check_polygon([-2, 5, 6]) + Traceback (most recent call last): + ... + ValueError: All values must be greater than 0 """ - if not nums: - raise ValueError("List is invalid") + if len(nums) < 2: + raise ValueError("List must have at least two values") + if any(i <= 0 for i in 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]) From a438b35298639fbea70ef0997a1953cd0a33d17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 14:35:30 -0500 Subject: [PATCH 7/9] Split doctest in multiples lines --- maths/check_polygon.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 0283803bad51..4215cf9ebb7f 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -16,8 +16,9 @@ def check_polygon(nums: list[float]) -> bool: False >>> check_polygon([1, 4.3, 5.2, 12.2]) False - >>> nums = [3, 7, 13, 2];check_polygon(nums); nums - 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([]) Traceback (most recent call last): From 981392696189fe069089c0fc8661f736806a9f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 14:38:10 -0500 Subject: [PATCH 8/9] Change ValueError to Monogons and Digons are not poly --- maths/check_polygon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 4215cf9ebb7f..911ae6bc97c4 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -30,7 +30,7 @@ def check_polygon(nums: list[float]) -> bool: ValueError: All values must be greater than 0 """ if len(nums) < 2: - raise ValueError("List must have at least two values") + raise ValueError("Monogons and Digons are not polygons in the Euclidean space") if any(i <= 0 for i in nums): raise ValueError("All values must be greater than 0") copy_nums = nums.copy() From dd6455e5e2a461360fd4c732747d0b9fb04b15e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Osorio=20L=C3=B3pez?= Date: Wed, 20 Oct 2021 15:30:09 -0500 Subject: [PATCH 9/9] Correct doctest refering number of sides --- maths/check_polygon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/check_polygon.py b/maths/check_polygon.py index 911ae6bc97c4..1e8dce7183ad 100644 --- a/maths/check_polygon.py +++ b/maths/check_polygon.py @@ -23,7 +23,7 @@ def check_polygon(nums: list[float]) -> bool: >>> check_polygon([]) Traceback (most recent call last): ... - ValueError: List must have at least two values + ValueError: Monogons and Digons are not polygons in the Euclidean space >>> check_polygon([-2, 5, 6]) Traceback (most recent call last): ...