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