From 7d139ee7f1e48648cc8cf176b293d23d2ba85d13 Mon Sep 17 00:00:00 2001 From: Caeden Perelli-Harris Date: Tue, 1 Nov 2022 06:50:43 +0000 Subject: [PATCH] refactor(abs): Condense `abs_min` and `abs_max` (#7881) * refactor(abs): Condense `abs_min` and `abs_max` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- maths/abs.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ maths/abs_max.py | 50 ------------------------------------ maths/abs_min.py | 35 ------------------------- 3 files changed, 66 insertions(+), 85 deletions(-) delete mode 100644 maths/abs_max.py delete mode 100644 maths/abs_min.py diff --git a/maths/abs.py b/maths/abs.py index dfea52dfb..cb0ffc8a5 100644 --- a/maths/abs.py +++ b/maths/abs.py @@ -15,6 +15,62 @@ def abs_val(num: float) -> float: return -num if num < 0 else num +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 abs_max(x: list[int]) -> int: + """ + >>> abs_max([0,5,1,11]) + 11 + >>> abs_max([3,-10,-2]) + -10 + >>> abs_max([]) + Traceback (most recent call last): + ... + ValueError: abs_max() arg is an empty sequence + """ + if len(x) == 0: + raise ValueError("abs_max() arg is an empty sequence") + j = x[0] + for i in x: + if abs(i) > abs(j): + j = i + return j + + +def abs_max_sort(x: list[int]) -> int: + """ + >>> abs_max_sort([0,5,1,11]) + 11 + >>> abs_max_sort([3,-10,-2]) + -10 + >>> abs_max_sort([]) + Traceback (most recent call last): + ... + ValueError: abs_max_sort() arg is an empty sequence + """ + if len(x) == 0: + raise ValueError("abs_max_sort() arg is an empty sequence") + return sorted(x, key=abs)[-1] + + def test_abs_val(): """ >>> test_abs_val() @@ -23,6 +79,16 @@ def test_abs_val(): assert 34 == abs_val(34) assert 100000000000 == abs_val(-100000000000) + a = [-3, -1, 2, -11] + assert abs_max(a) == -11 + assert abs_max_sort(a) == -11 + assert abs_min(a) == -1 + if __name__ == "__main__": + import doctest + + doctest.testmod() + + test_abs_val() print(abs_val(-34)) # --> 34 diff --git a/maths/abs_max.py b/maths/abs_max.py deleted file mode 100644 index 4a4b4d9eb..000000000 --- a/maths/abs_max.py +++ /dev/null @@ -1,50 +0,0 @@ -from __future__ import annotations - - -def abs_max(x: list[int]) -> int: - """ - >>> abs_max([0,5,1,11]) - 11 - >>> abs_max([3,-10,-2]) - -10 - >>> abs_max([]) - Traceback (most recent call last): - ... - ValueError: abs_max() arg is an empty sequence - """ - if len(x) == 0: - raise ValueError("abs_max() arg is an empty sequence") - j = x[0] - for i in x: - if abs(i) > abs(j): - j = i - return j - - -def abs_max_sort(x: list[int]) -> int: - """ - >>> abs_max_sort([0,5,1,11]) - 11 - >>> abs_max_sort([3,-10,-2]) - -10 - >>> abs_max_sort([]) - Traceback (most recent call last): - ... - ValueError: abs_max_sort() arg is an empty sequence - """ - if len(x) == 0: - raise ValueError("abs_max_sort() arg is an empty sequence") - 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__": - import doctest - - doctest.testmod(verbose=True) - main() diff --git a/maths/abs_min.py b/maths/abs_min.py deleted file mode 100644 index 00dbcb025..000000000 --- a/maths/abs_min.py +++ /dev/null @@ -1,35 +0,0 @@ -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()