2020-09-23 11:30:13 +00:00
|
|
|
from __future__ import annotations
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-07-11 04:43:03 +00:00
|
|
|
|
2020-09-23 11:30:13 +00:00
|
|
|
def abs_max(x: list[int]) -> int:
|
2018-11-03 20:08:13 +00:00
|
|
|
"""
|
2019-07-11 04:43:03 +00:00
|
|
|
>>> abs_max([0,5,1,11])
|
2018-11-03 20:08:13 +00:00
|
|
|
11
|
2019-07-11 04:43:03 +00:00
|
|
|
>>> abs_max([3,-10,-2])
|
2018-11-03 20:08:13 +00:00
|
|
|
-10
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
j = x[0]
|
2018-11-03 20:08:13 +00:00
|
|
|
for i in x:
|
2019-02-15 16:01:10 +00:00
|
|
|
if abs(i) > abs(j):
|
2018-11-03 20:08:13 +00:00
|
|
|
j = i
|
|
|
|
return j
|
2019-02-15 16:01:10 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-07-11 04:43:03 +00:00
|
|
|
def abs_max_sort(x):
|
|
|
|
"""
|
|
|
|
>>> abs_max_sort([0,5,1,11])
|
|
|
|
11
|
|
|
|
>>> abs_max_sort([3,-10,-2])
|
|
|
|
-10
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
return sorted(x, key=abs)[-1]
|
|
|
|
|
2018-11-03 20:08:13 +00:00
|
|
|
|
|
|
|
def main():
|
2019-10-05 05:14:13 +00:00
|
|
|
a = [1, 2, -11]
|
2019-07-11 04:43:03 +00:00
|
|
|
assert abs_max(a) == -11
|
|
|
|
assert abs_max_sort(a) == -11
|
2018-11-03 20:08:13 +00:00
|
|
|
|
2018-12-04 17:18:32 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|