Skip to content

Commit a4d68d6

Browse files
authored
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
1 parent 90db983 commit a4d68d6

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

maths/abs_max.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,33 @@ def abs_max(x: list[int]) -> int:
77
11
88
>>> abs_max([3,-10,-2])
99
-10
10+
>>> abs_max([])
11+
Traceback (most recent call last):
12+
...
13+
ValueError: abs_max() arg is an empty sequence
1014
"""
15+
if len(x) == 0:
16+
raise ValueError("abs_max() arg is an empty sequence")
1117
j = x[0]
1218
for i in x:
1319
if abs(i) > abs(j):
1420
j = i
1521
return j
1622

1723

18-
def abs_max_sort(x):
24+
def abs_max_sort(x: list[int]) -> int:
1925
"""
2026
>>> abs_max_sort([0,5,1,11])
2127
11
2228
>>> abs_max_sort([3,-10,-2])
2329
-10
30+
>>> abs_max_sort([])
31+
Traceback (most recent call last):
32+
...
33+
ValueError: abs_max_sort() arg is an empty sequence
2434
"""
35+
if len(x) == 0:
36+
raise ValueError("abs_max_sort() arg is an empty sequence")
2537
return sorted(x, key=abs)[-1]
2638

2739

@@ -32,4 +44,7 @@ def main():
3244

3345

3446
if __name__ == "__main__":
47+
import doctest
48+
49+
doctest.testmod(verbose=True)
3550
main()

maths/abs_min.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
from __future__ import annotations
2+
13
from .abs import abs_val
24

35

4-
def absMin(x):
6+
def abs_min(x: list[int]) -> int:
57
"""
6-
>>> absMin([0,5,1,11])
8+
>>> abs_min([0,5,1,11])
79
0
8-
>>> absMin([3,-10,-2])
10+
>>> abs_min([3,-10,-2])
911
-2
12+
>>> abs_min([])
13+
Traceback (most recent call last):
14+
...
15+
ValueError: abs_min() arg is an empty sequence
1016
"""
17+
if len(x) == 0:
18+
raise ValueError("abs_min() arg is an empty sequence")
1119
j = x[0]
1220
for i in x:
1321
if abs_val(i) < abs_val(j):
@@ -17,8 +25,11 @@ def absMin(x):
1725

1826
def main():
1927
a = [-3, -1, 2, -11]
20-
print(absMin(a)) # = -1
28+
print(abs_min(a)) # = -1
2129

2230

2331
if __name__ == "__main__":
32+
import doctest
33+
34+
doctest.testmod(verbose=True)
2435
main()

0 commit comments

Comments
 (0)