mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
9200a2e543
* from __future__ import annotations * fixup! from __future__ import annotations * fixup! from __future__ import annotations * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
36 lines
537 B
Python
36 lines
537 B
Python
from __future__ import annotations
|
|
|
|
|
|
def abs_max(x: list[int]) -> int:
|
|
"""
|
|
>>> abs_max([0,5,1,11])
|
|
11
|
|
>>> abs_max([3,-10,-2])
|
|
-10
|
|
"""
|
|
j = x[0]
|
|
for i in x:
|
|
if abs(i) > abs(j):
|
|
j = i
|
|
return j
|
|
|
|
|
|
def abs_max_sort(x):
|
|
"""
|
|
>>> abs_max_sort([0,5,1,11])
|
|
11
|
|
>>> abs_max_sort([3,-10,-2])
|
|
-10
|
|
"""
|
|
return sorted(x, key=abs)[-1]
|
|
|
|
|
|
def main():
|
|
a = [1, 2, -11]
|
|
assert abs_max(a) == -11
|
|
assert abs_max_sort(a) == -11
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|