File tree 2 files changed +31
-5
lines changed
2 files changed +31
-5
lines changed Original file line number Diff line number Diff line change @@ -7,21 +7,33 @@ def abs_max(x: list[int]) -> int:
7
7
11
8
8
>>> abs_max([3,-10,-2])
9
9
-10
10
+ >>> abs_max([])
11
+ Traceback (most recent call last):
12
+ ...
13
+ ValueError: abs_max() arg is an empty sequence
10
14
"""
15
+ if len (x ) == 0 :
16
+ raise ValueError ("abs_max() arg is an empty sequence" )
11
17
j = x [0 ]
12
18
for i in x :
13
19
if abs (i ) > abs (j ):
14
20
j = i
15
21
return j
16
22
17
23
18
- def abs_max_sort (x ) :
24
+ def abs_max_sort (x : list [ int ]) -> int :
19
25
"""
20
26
>>> abs_max_sort([0,5,1,11])
21
27
11
22
28
>>> abs_max_sort([3,-10,-2])
23
29
-10
30
+ >>> abs_max_sort([])
31
+ Traceback (most recent call last):
32
+ ...
33
+ ValueError: abs_max_sort() arg is an empty sequence
24
34
"""
35
+ if len (x ) == 0 :
36
+ raise ValueError ("abs_max_sort() arg is an empty sequence" )
25
37
return sorted (x , key = abs )[- 1 ]
26
38
27
39
@@ -32,4 +44,7 @@ def main():
32
44
33
45
34
46
if __name__ == "__main__" :
47
+ import doctest
48
+
49
+ doctest .testmod (verbose = True )
35
50
main ()
Original file line number Diff line number Diff line change
1
+ from __future__ import annotations
2
+
1
3
from .abs import abs_val
2
4
3
5
4
- def absMin ( x ) :
6
+ def abs_min ( x : list [ int ]) -> int :
5
7
"""
6
- >>> absMin ([0,5,1,11])
8
+ >>> abs_min ([0,5,1,11])
7
9
0
8
- >>> absMin ([3,-10,-2])
10
+ >>> abs_min ([3,-10,-2])
9
11
-2
12
+ >>> abs_min([])
13
+ Traceback (most recent call last):
14
+ ...
15
+ ValueError: abs_min() arg is an empty sequence
10
16
"""
17
+ if len (x ) == 0 :
18
+ raise ValueError ("abs_min() arg is an empty sequence" )
11
19
j = x [0 ]
12
20
for i in x :
13
21
if abs_val (i ) < abs_val (j ):
@@ -17,8 +25,11 @@ def absMin(x):
17
25
18
26
def main ():
19
27
a = [- 3 , - 1 , 2 , - 11 ]
20
- print (absMin (a )) # = -1
28
+ print (abs_min (a )) # = -1
21
29
22
30
23
31
if __name__ == "__main__" :
32
+ import doctest
33
+
34
+ doctest .testmod (verbose = True )
24
35
main ()
You can’t perform that action at this time.
0 commit comments