mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +08:00
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:
@ -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)
|
||||
|
Reference in New Issue
Block a user