mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 06:48:09 +00:00
bugfix: Add abs_max.py & abs_min.py empty list detection (#4844)
* bugfix: Add abs_max.py & abs_min.py empty list detection * fix shebangs check
This commit is contained in:
parent
90db98304e
commit
a4d68d69f1
|
@ -7,7 +7,13 @@ def abs_max(x: list[int]) -> int:
|
|||
11
|
||||
>>> abs_max([3,-10,-2])
|
||||
-10
|
||||
>>> abs_max([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: abs_max() arg is an empty sequence
|
||||
"""
|
||||
if len(x) == 0:
|
||||
raise ValueError("abs_max() arg is an empty sequence")
|
||||
j = x[0]
|
||||
for i in x:
|
||||
if abs(i) > abs(j):
|
||||
|
@ -15,13 +21,19 @@ def abs_max(x: list[int]) -> int:
|
|||
return j
|
||||
|
||||
|
||||
def abs_max_sort(x):
|
||||
def abs_max_sort(x: list[int]) -> int:
|
||||
"""
|
||||
>>> abs_max_sort([0,5,1,11])
|
||||
11
|
||||
>>> abs_max_sort([3,-10,-2])
|
||||
-10
|
||||
>>> abs_max_sort([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: abs_max_sort() arg is an empty sequence
|
||||
"""
|
||||
if len(x) == 0:
|
||||
raise ValueError("abs_max_sort() arg is an empty sequence")
|
||||
return sorted(x, key=abs)[-1]
|
||||
|
||||
|
||||
|
@ -32,4 +44,7 @@ def main():
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod(verbose=True)
|
||||
main()
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from .abs import abs_val
|
||||
|
||||
|
||||
def absMin(x):
|
||||
def abs_min(x: list[int]) -> int:
|
||||
"""
|
||||
>>> absMin([0,5,1,11])
|
||||
>>> abs_min([0,5,1,11])
|
||||
0
|
||||
>>> absMin([3,-10,-2])
|
||||
>>> abs_min([3,-10,-2])
|
||||
-2
|
||||
>>> abs_min([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: abs_min() arg is an empty sequence
|
||||
"""
|
||||
if len(x) == 0:
|
||||
raise ValueError("abs_min() arg is an empty sequence")
|
||||
j = x[0]
|
||||
for i in x:
|
||||
if abs_val(i) < abs_val(j):
|
||||
|
@ -17,8 +25,11 @@ def absMin(x):
|
|||
|
||||
def main():
|
||||
a = [-3, -1, 2, -11]
|
||||
print(absMin(a)) # = -1
|
||||
print(abs_min(a)) # = -1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod(verbose=True)
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user