From 77b243e62b09cdf6201916af6762b03c54d8f77a Mon Sep 17 00:00:00 2001 From: Lewis Tian Date: Thu, 7 Oct 2021 23:20:32 +0800 Subject: [PATCH] bugfix: Add empty list detection for find_max/min (#4881) * bugfix: Add empty list detection for find_max/min * fix shebangs check --- maths/find_max.py | 20 ++++++++++++------- maths/find_max_recursion.py | 39 ++++++++++++++++++++++++++++++++++--- maths/find_min.py | 21 ++++++++++++++------ maths/find_min_recursion.py | 39 ++++++++++++++++++++++++++++++++++--- 4 files changed, 100 insertions(+), 19 deletions(-) diff --git a/maths/find_max.py b/maths/find_max.py index 4d92e37eb..684fbe816 100644 --- a/maths/find_max.py +++ b/maths/find_max.py @@ -1,7 +1,7 @@ -# NguyenU +from __future__ import annotations -def find_max(nums): +def find_max(nums: list[int | float]) -> int | float: """ >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): ... find_max(nums) == max(nums) @@ -9,7 +9,15 @@ def find_max(nums): True True True + >>> find_max([2, 4, 9, 7, 19, 94, 5]) + 94 + >>> find_max([]) + Traceback (most recent call last): + ... + ValueError: find_max() arg is an empty sequence """ + if len(nums) == 0: + raise ValueError("find_max() arg is an empty sequence") max_num = nums[0] for x in nums: if x > max_num: @@ -17,9 +25,7 @@ def find_max(nums): return max_num -def main(): - print(find_max([2, 4, 9, 7, 19, 94, 5])) # 94 - - if __name__ == "__main__": - main() + import doctest + + doctest.testmod(verbose=True) diff --git a/maths/find_max_recursion.py b/maths/find_max_recursion.py index 03fb81950..629932e08 100644 --- a/maths/find_max_recursion.py +++ b/maths/find_max_recursion.py @@ -1,5 +1,8 @@ +from __future__ import annotations + + # Divide and Conquer algorithm -def find_max(nums, left, right): +def find_max(nums: list[int | float], left: int, right: int) -> int | float: """ find max value in list :param nums: contains elements @@ -7,10 +10,39 @@ def find_max(nums, left, right): :param right: index of last element :return: max in nums + >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): + ... find_max(nums, 0, len(nums) - 1) == max(nums) + True + True + True + True >>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] >>> find_max(nums, 0, len(nums) - 1) == max(nums) True + >>> find_max([], 0, 0) + Traceback (most recent call last): + ... + ValueError: find_max() arg is an empty sequence + >>> find_max(nums, 0, len(nums)) == max(nums) + Traceback (most recent call last): + ... + IndexError: list index out of range + >>> find_max(nums, -len(nums), -1) == max(nums) + True + >>> find_max(nums, -len(nums) - 1, -1) == max(nums) + Traceback (most recent call last): + ... + IndexError: list index out of range """ + if len(nums) == 0: + raise ValueError("find_max() arg is an empty sequence") + if ( + left >= len(nums) + or left < -len(nums) + or right >= len(nums) + or right < -len(nums) + ): + raise IndexError("list index out of range") if left == right: return nums[left] mid = (left + right) >> 1 # the middle @@ -21,5 +53,6 @@ def find_max(nums, left, right): if __name__ == "__main__": - nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] - assert find_max(nums, 0, len(nums) - 1) == 10 + import doctest + + doctest.testmod(verbose=True) diff --git a/maths/find_min.py b/maths/find_min.py index 2af2e44ba..228205ed7 100644 --- a/maths/find_min.py +++ b/maths/find_min.py @@ -1,4 +1,7 @@ -def find_min(nums): +from __future__ import annotations + + +def find_min(nums: list[int | float]) -> int | float: """ Find Minimum Number in a List :param nums: contains elements @@ -10,7 +13,15 @@ def find_min(nums): True True True + >>> find_min([0, 1, 2, 3, 4, 5, -3, 24, -56]) + -56 + >>> find_min([]) + Traceback (most recent call last): + ... + ValueError: find_min() arg is an empty sequence """ + if len(nums) == 0: + raise ValueError("find_min() arg is an empty sequence") min_num = nums[0] for num in nums: if min_num > num: @@ -18,9 +29,7 @@ def find_min(nums): return min_num -def main(): - assert find_min([0, 1, 2, 3, 4, 5, -3, 24, -56]) == -56 - - if __name__ == "__main__": - main() + import doctest + + doctest.testmod(verbose=True) diff --git a/maths/find_min_recursion.py b/maths/find_min_recursion.py index 4488967cc..4d11015ef 100644 --- a/maths/find_min_recursion.py +++ b/maths/find_min_recursion.py @@ -1,5 +1,8 @@ +from __future__ import annotations + + # Divide and Conquer algorithm -def find_min(nums, left, right): +def find_min(nums: list[int | float], left: int, right: int) -> int | float: """ find min value in list :param nums: contains elements @@ -7,10 +10,39 @@ def find_min(nums, left, right): :param right: index of last element :return: min in nums + >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): + ... find_min(nums, 0, len(nums) - 1) == min(nums) + True + True + True + True >>> nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] >>> find_min(nums, 0, len(nums) - 1) == min(nums) True + >>> find_min([], 0, 0) + Traceback (most recent call last): + ... + ValueError: find_min() arg is an empty sequence + >>> find_min(nums, 0, len(nums)) == min(nums) + Traceback (most recent call last): + ... + IndexError: list index out of range + >>> find_min(nums, -len(nums), -1) == min(nums) + True + >>> find_min(nums, -len(nums) - 1, -1) == min(nums) + Traceback (most recent call last): + ... + IndexError: list index out of range """ + if len(nums) == 0: + raise ValueError("find_min() arg is an empty sequence") + if ( + left >= len(nums) + or left < -len(nums) + or right >= len(nums) + or right < -len(nums) + ): + raise IndexError("list index out of range") if left == right: return nums[left] mid = (left + right) >> 1 # the middle @@ -21,5 +53,6 @@ def find_min(nums, left, right): if __name__ == "__main__": - nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10] - assert find_min(nums, 0, len(nums) - 1) == 1 + import doctest + + doctest.testmod(verbose=True)