forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabs_min.py
More file actions
35 lines (27 loc) · 646 Bytes
/
abs_min.py
File metadata and controls
35 lines (27 loc) · 646 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations
from .abs import abs_val
def abs_min(x: list[int]) -> int:
"""
>>> abs_min([0,5,1,11])
0
>>> 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):
j = i
return j
def main():
a = [-3, -1, 2, -11]
print(abs_min(a)) # = -1
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
main()