2021-10-06 14:06:49 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-07-21 06:16:28 +00:00
|
|
|
from .abs import abs_val
|
|
|
|
|
2019-07-11 16:20:41 +00:00
|
|
|
|
2021-10-06 14:06:49 +00:00
|
|
|
def abs_min(x: list[int]) -> int:
|
2018-11-03 20:08:13 +00:00
|
|
|
"""
|
2021-10-06 14:06:49 +00:00
|
|
|
>>> abs_min([0,5,1,11])
|
2018-11-03 20:08:13 +00:00
|
|
|
0
|
2021-10-06 14:06:49 +00:00
|
|
|
>>> abs_min([3,-10,-2])
|
2018-11-03 20:08:13 +00:00
|
|
|
-2
|
2021-10-06 14:06:49 +00:00
|
|
|
>>> abs_min([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: abs_min() arg is an empty sequence
|
2018-11-03 20:08:13 +00:00
|
|
|
"""
|
2021-10-06 14:06:49 +00:00
|
|
|
if len(x) == 0:
|
|
|
|
raise ValueError("abs_min() arg is an empty sequence")
|
2018-12-04 17:18:32 +00:00
|
|
|
j = x[0]
|
2018-11-03 20:08:13 +00:00
|
|
|
for i in x:
|
2019-07-11 16:20:41 +00:00
|
|
|
if abs_val(i) < abs_val(j):
|
2018-11-03 20:08:13 +00:00
|
|
|
j = i
|
|
|
|
return j
|
|
|
|
|
2019-07-21 06:16:28 +00:00
|
|
|
|
2018-11-03 20:08:13 +00:00
|
|
|
def main():
|
2019-10-05 05:14:13 +00:00
|
|
|
a = [-3, -1, 2, -11]
|
2021-10-06 14:06:49 +00:00
|
|
|
print(abs_min(a)) # = -1
|
2018-11-03 20:08:13 +00:00
|
|
|
|
2019-07-21 06:16:28 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2021-10-06 14:06:49 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod(verbose=True)
|
2019-10-05 05:14:13 +00:00
|
|
|
main()
|