bugfix: Add empty list detection for find_max/min (#4881)

* bugfix: Add empty list detection for find_max/min

* fix shebangs check
This commit is contained in:
Lewis Tian
2021-10-07 23:20:32 +08:00
committed by GitHub
parent d324f91fe7
commit 77b243e62b
4 changed files with 100 additions and 19 deletions

View File

@ -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)