From 09a4e3e10fc1e3d01c6f1eee982e25fd08b5d520 Mon Sep 17 00:00:00 2001 From: kmtGryffindor20 Date: Sat, 14 Oct 2023 12:58:14 +0530 Subject: [PATCH 1/5] [ADDED] Implementation of Geometric Mean. --- maths/geometric_mean.py | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 maths/geometric_mean.py diff --git a/maths/geometric_mean.py b/maths/geometric_mean.py new file mode 100644 index 000000000000..e208c99aae1c --- /dev/null +++ b/maths/geometric_mean.py @@ -0,0 +1,52 @@ +""" +The Geometric Mean of n numbers is defined as the n-th root of the product +of those numbers. It is used to measure the central tendenct of the numbers. +https://en.wikipedia.org/wiki/Geometric_mean +""" +def compute_geometric_mean(*args: float) -> float: + """ + Return the geometric mean of the argument numbers. + >>> compute_geometric_mean(2,8) + 4.0 + >>> compute_geometric_mean('a', 4) + Traceback (most recent call last): + ... + TypeError: Not a Number + >>> compute_geometric_mean(5, 125) + 25.0 + >>> compute_geometric_mean(1, 0) + 0.0 + >>> compute_geometric_mean(1, 5, 25, 5) + 5.0 + >>> compute_geometric_mean(2, -2) + Traceback (most recent call last): + ... + ArithmeticError: Cannot Compute Geometric Mean for these numbers. + >>> compute_geometric_mean(-5, 25, 1) + -5.0 + """ + product = 1 + for number in args: + if (not isinstance(number, int)) and (not isinstance(number, float)) : + raise TypeError("Not a Number") + product *= number + # Cannot calculate the even root for negative product. + # Frequently they are restricted to being positive. + if product < 0 and len(args) % 2 == 0: + raise ArithmeticError("Cannot Compute Geometric Mean for these numbers.") + mean = abs(product)**(1./len(args)) + # Since python calculates complex roots for negative products with odd roots. + if product < 0: + mean = -mean + # Since it does floating point arithmetic, it gives 64**(1/3) as 3.99999996 + possible_mean = float(round(mean)) + # To check if the rounded number is actually the mean. + if possible_mean**len(args) == product: + mean = possible_mean + return mean + + +if __name__ == '__main__': + from doctest import testmod + testmod(name='compute_geometric_mean') + print(compute_geometric_mean(-3, -27)) From 9ebe2fd66d6b55a76771195ba5fe9ab80b807fdb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Oct 2023 07:32:37 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/geometric_mean.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/maths/geometric_mean.py b/maths/geometric_mean.py index e208c99aae1c..ee0fef2f36ff 100644 --- a/maths/geometric_mean.py +++ b/maths/geometric_mean.py @@ -3,6 +3,8 @@ of those numbers. It is used to measure the central tendenct of the numbers. https://en.wikipedia.org/wiki/Geometric_mean """ + + def compute_geometric_mean(*args: float) -> float: """ Return the geometric mean of the argument numbers. @@ -27,26 +29,27 @@ def compute_geometric_mean(*args: float) -> float: """ product = 1 for number in args: - if (not isinstance(number, int)) and (not isinstance(number, float)) : + if (not isinstance(number, int)) and (not isinstance(number, float)): raise TypeError("Not a Number") product *= number # Cannot calculate the even root for negative product. # Frequently they are restricted to being positive. if product < 0 and len(args) % 2 == 0: raise ArithmeticError("Cannot Compute Geometric Mean for these numbers.") - mean = abs(product)**(1./len(args)) + mean = abs(product) ** (1.0 / len(args)) # Since python calculates complex roots for negative products with odd roots. if product < 0: mean = -mean # Since it does floating point arithmetic, it gives 64**(1/3) as 3.99999996 possible_mean = float(round(mean)) # To check if the rounded number is actually the mean. - if possible_mean**len(args) == product: + if possible_mean ** len(args) == product: mean = possible_mean return mean -if __name__ == '__main__': +if __name__ == "__main__": from doctest import testmod - testmod(name='compute_geometric_mean') + + testmod(name="compute_geometric_mean") print(compute_geometric_mean(-3, -27)) From 0c15acf98663c0629be9264fafb0922730198b4a Mon Sep 17 00:00:00 2001 From: kmtGryffindor20 Date: Sat, 14 Oct 2023 13:03:38 +0530 Subject: [PATCH 3/5] Rectified type hints --- maths/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/geometric_mean.py b/maths/geometric_mean.py index e208c99aae1c..93d68ae2ae08 100644 --- a/maths/geometric_mean.py +++ b/maths/geometric_mean.py @@ -3,7 +3,7 @@ of those numbers. It is used to measure the central tendenct of the numbers. https://en.wikipedia.org/wiki/Geometric_mean """ -def compute_geometric_mean(*args: float) -> float: +def compute_geometric_mean(*args: float or int) -> float: """ Return the geometric mean of the argument numbers. >>> compute_geometric_mean(2,8) From 22a3750e7b65d63dc00fa47df7fd14b320a27d99 Mon Sep 17 00:00:00 2001 From: kmtGryffindor20 Date: Sat, 14 Oct 2023 13:12:00 +0530 Subject: [PATCH 4/5] Typo --- maths/geometric_mean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/geometric_mean.py b/maths/geometric_mean.py index 81ae15fe2bab..d60678dce2b7 100644 --- a/maths/geometric_mean.py +++ b/maths/geometric_mean.py @@ -1,11 +1,11 @@ """ The Geometric Mean of n numbers is defined as the n-th root of the product -of those numbers. It is used to measure the central tendenct of the numbers. +of those numbers. It is used to measure the central tendency of the numbers. https://en.wikipedia.org/wiki/Geometric_mean """ -def compute_geometric_mean(*args: float or int) -> float: +def compute_geometric_mean(*args: int) -> float: """ Return the geometric mean of the argument numbers. >>> compute_geometric_mean(2,8) From 99f8bbf7e438ae487e71b4dc5fe4c72d67bb76f0 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Mon, 30 Dec 2024 18:13:58 -0800 Subject: [PATCH 5/5] Apply suggestions from code review --- maths/geometric_mean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/geometric_mean.py b/maths/geometric_mean.py index d60678dce2b7..240d519ad398 100644 --- a/maths/geometric_mean.py +++ b/maths/geometric_mean.py @@ -29,14 +29,14 @@ def compute_geometric_mean(*args: int) -> float: """ product = 1 for number in args: - if (not isinstance(number, int)) and (not isinstance(number, float)): + if not isinstance(number, int) and not isinstance(number, float): raise TypeError("Not a Number") product *= number # Cannot calculate the even root for negative product. # Frequently they are restricted to being positive. if product < 0 and len(args) % 2 == 0: raise ArithmeticError("Cannot Compute Geometric Mean for these numbers.") - mean = abs(product) ** (1.0 / len(args)) + mean = abs(product) ** (1 / len(args)) # Since python calculates complex roots for negative products with odd roots. if product < 0: mean = -mean