From 5b874a65b6985074ddad541729f297f7b99a0de4 Mon Sep 17 00:00:00 2001 From: Joydeep Das Date: Fri, 17 Oct 2025 00:11:17 +0530 Subject: [PATCH 1/2] To Add Arithmetic Mean function in maths/series --- maths/series/maths/series/arithmetic_mean.py | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 maths/series/maths/series/arithmetic_mean.py diff --git a/maths/series/maths/series/arithmetic_mean.py b/maths/series/maths/series/arithmetic_mean.py new file mode 100644 index 000000000000..d6f3520e48ac --- /dev/null +++ b/maths/series/maths/series/arithmetic_mean.py @@ -0,0 +1,48 @@ +""" +The Arithmetic Mean of n numbers is defined as the sum of the numbers +divided by n. It is used to measure the central tendency of the numbers. +https://en.wikipedia.org/wiki/Arithmetic_mean +""" + + +def compute_arithmetic_mean(*args: float) -> float: + """ + Return the arithmetic mean of the argument numbers. + If invalid input is provided, it prints an error message and returns None. + + >>> compute_arithmetic_mean(1, 2, 3, 4, 5) + 3.0 + >>> compute_arithmetic_mean(5, 10) + 7.5 + >>> compute_arithmetic_mean('a', 69) + 'Error: Not a Number' + >>> compute_arithmetic_mean() + 'Error: At least one number is required' + >>> compute_arithmetic_mean(2.5, 3.5, 4.0) + 3.3333333333333335 + """ + try: + if len(args) == 0: + raise ValueError("At least one number is required") + + total = 0 + count = 0 + for number in args: + if not isinstance(number, (int, float)): + raise TypeError("Not a Number") + total += number + count += 1 + + return total / count + + except (TypeError, ValueError) as error: + return f"Error: {error}" + + +if __name__ == "__main__": + from doctest import testmod + + testmod(name="compute_arithmetic_mean") + print(compute_arithmetic_mean(1, 2, 3, 4, 5)) + print(compute_arithmetic_mean('a', 69)) + print(compute_arithmetic_mean()) From ebe8cf782ebf2fee683add9bdfbc300f43d132ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Oct 2025 18:50:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/series/maths/series/arithmetic_mean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/series/maths/series/arithmetic_mean.py b/maths/series/maths/series/arithmetic_mean.py index d6f3520e48ac..ef3521c4a2c8 100644 --- a/maths/series/maths/series/arithmetic_mean.py +++ b/maths/series/maths/series/arithmetic_mean.py @@ -38,11 +38,11 @@ def compute_arithmetic_mean(*args: float) -> float: except (TypeError, ValueError) as error: return f"Error: {error}" - + if __name__ == "__main__": from doctest import testmod testmod(name="compute_arithmetic_mean") print(compute_arithmetic_mean(1, 2, 3, 4, 5)) - print(compute_arithmetic_mean('a', 69)) + print(compute_arithmetic_mean("a", 69)) print(compute_arithmetic_mean())