From 74045b64f0acd2d5bfc95b3277203ad588a58730 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Sun, 28 Feb 2021 23:32:19 +0530 Subject: [PATCH 1/7] arithmetic_mean --- maths/series/arithmetic_mean.py | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 maths/series/arithmetic_mean.py diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py new file mode 100644 index 000000000000..9d341feac13d --- /dev/null +++ b/maths/series/arithmetic_mean.py @@ -0,0 +1,55 @@ +def is_arithmetic_series(series: list) -> bool: + """ + checking whether the input series is arithmetic series or not + + >>> is_arithmetic_series([2, 4, 6]) + True + >>> is_arithmetic_series([3, 6, 12, 24]) + False + >>> is_arithmetic_series([1, 2, 3]) + True + + """ + if len(series) == 1: + return True + common_diff = series[1] - series[0] + for index in range(len(series) - 1): + if series[index + 1] - series[index] != common_diff: + return False + return True + + +def arithmetic_mean(series: list) -> float: + """ + return the geometric mean of series + + >>> arithmetic_mean([2, 4, 6]) + 4.0 + >>> arithmetic_mean([3, 6, 9, 12]) + 7.5 + >>> arithmetic_mean([4, 8, 1]) + Traceback (most recent call last): + ... + ValueError: Input list is not an arithmetic series + >>> arithmetic_mean([1, 2, 3]) + 2.0 + >>> arithmetic_mean([]) + Traceback (most recent call last): + ... + ValueError: Input list must be a non empty list + + """ + if len(series) == 0: + raise ValueError("Input list must be a non empty list") + if not is_arithmetic_series(series): + raise ValueError("Input list is not an arithmetic series") + answer = 0 + for _ in series: + answer += _ + return answer / len(series) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 3b562a8507f2b1c75b3e9358bcaead2358c17464 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Sun, 28 Feb 2021 23:33:26 +0530 Subject: [PATCH 2/7] arithmetic_mean --- maths/series/arithmetic_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py index 9d341feac13d..994f479d165e 100644 --- a/maths/series/arithmetic_mean.py +++ b/maths/series/arithmetic_mean.py @@ -21,7 +21,7 @@ def is_arithmetic_series(series: list) -> bool: def arithmetic_mean(series: list) -> float: """ - return the geometric mean of series + return the arithmetic mean of series >>> arithmetic_mean([2, 4, 6]) 4.0 From 6deb893d4ff813627fb9ab4decf2df572c9728dc Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:53:15 +0530 Subject: [PATCH 3/7] checks --- maths/series/arithmetic_mean.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py index 994f479d165e..2519a8a35c35 100644 --- a/maths/series/arithmetic_mean.py +++ b/maths/series/arithmetic_mean.py @@ -27,6 +27,10 @@ def arithmetic_mean(series: list) -> float: 4.0 >>> arithmetic_mean([3, 6, 9, 12]) 7.5 + >>> arithmetic_mean(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 6] >>> arithmetic_mean([4, 8, 1]) Traceback (most recent call last): ... @@ -39,13 +43,15 @@ def arithmetic_mean(series: list) -> float: ValueError: Input list must be a non empty list """ + if not isinstance(series, list): + raise ValueError("Input series is not valid, valid series - [2, 4, 6]") if len(series) == 0: raise ValueError("Input list must be a non empty list") if not is_arithmetic_series(series): raise ValueError("Input list is not an arithmetic series") answer = 0 - for _ in series: - answer += _ + for val in series: + answer += val return answer / len(series) From 3913a39ae2c4ee183443eed67ee7427e3d322ad4 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:57:58 +0530 Subject: [PATCH 4/7] checked --- maths/series/geometric_mean.py | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 maths/series/geometric_mean.py diff --git a/maths/series/geometric_mean.py b/maths/series/geometric_mean.py new file mode 100644 index 000000000000..50ae54ad6574 --- /dev/null +++ b/maths/series/geometric_mean.py @@ -0,0 +1,75 @@ +""" +GEOMETRIC MEAN : https://en.wikipedia.org/wiki/Geometric_mean +""" + + +def is_geometric_series(series: list) -> bool: + """ + checking whether the input series is geometric series or not + + >>> is_geometric_series([2, 4, 8]) + True + >>> is_geometric_series([3, 6, 12, 24]) + True + >>> is_geometric_series([1, 2, 3]) + False + >>> is_geometric_series([0, 0, 3]) + False + + """ + if len(series) == 1: + return True + try: + common_ratio = series[1] / series[0] + for index in range(len(series) - 1): + if series[index + 1] / series[index] != common_ratio: + return False + except ZeroDivisionError: + return False + return True + + +def geometric_mean(series: list) -> float: + """ + return the geometric mean of series + + >>> geometric_mean([2, 4, 8]) + 3.9999999999999996 + >>> geometric_mean([3, 6, 12, 24]) + 8.48528137423857 + >>> geometric_mean([4, 8, 16]) + 7.999999999999999 + >>> geometric_mean(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 8] + >>> geometric_mean([1, 2, 3]) + Traceback (most recent call last): + ... + ValueError: Input list is not a geometric series + >>> geometric_mean([0, 2, 3]) + Traceback (most recent call last): + ... + ValueError: Input list is not a geometric series + >>> geometric_mean([]) + Traceback (most recent call last): + ... + ValueError: Input list must be a non empty list + + """ + if not isinstance(series, list): + raise ValueError("Input series is not valid, valid series - [2, 4, 8]") + if len(series) == 0: + raise ValueError("Input list must be a non empty list") + if not is_geometric_series(series): + raise ValueError("Input list is not a geometric series") + answer = 1 + for value in series: + answer *= value + return pow(answer, 1 / len(series)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 157ece614c3abf59ae1e543e462a18e949c04328 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 22:18:04 +0530 Subject: [PATCH 5/7] Revert "checked" This reverts commit 3913a39ae2c4ee183443eed67ee7427e3d322ad4. --- maths/series/geometric_mean.py | 75 ---------------------------------- 1 file changed, 75 deletions(-) delete mode 100644 maths/series/geometric_mean.py diff --git a/maths/series/geometric_mean.py b/maths/series/geometric_mean.py deleted file mode 100644 index 50ae54ad6574..000000000000 --- a/maths/series/geometric_mean.py +++ /dev/null @@ -1,75 +0,0 @@ -""" -GEOMETRIC MEAN : https://en.wikipedia.org/wiki/Geometric_mean -""" - - -def is_geometric_series(series: list) -> bool: - """ - checking whether the input series is geometric series or not - - >>> is_geometric_series([2, 4, 8]) - True - >>> is_geometric_series([3, 6, 12, 24]) - True - >>> is_geometric_series([1, 2, 3]) - False - >>> is_geometric_series([0, 0, 3]) - False - - """ - if len(series) == 1: - return True - try: - common_ratio = series[1] / series[0] - for index in range(len(series) - 1): - if series[index + 1] / series[index] != common_ratio: - return False - except ZeroDivisionError: - return False - return True - - -def geometric_mean(series: list) -> float: - """ - return the geometric mean of series - - >>> geometric_mean([2, 4, 8]) - 3.9999999999999996 - >>> geometric_mean([3, 6, 12, 24]) - 8.48528137423857 - >>> geometric_mean([4, 8, 16]) - 7.999999999999999 - >>> geometric_mean(4) - Traceback (most recent call last): - ... - ValueError: Input series is not valid, valid series - [2, 4, 8] - >>> geometric_mean([1, 2, 3]) - Traceback (most recent call last): - ... - ValueError: Input list is not a geometric series - >>> geometric_mean([0, 2, 3]) - Traceback (most recent call last): - ... - ValueError: Input list is not a geometric series - >>> geometric_mean([]) - Traceback (most recent call last): - ... - ValueError: Input list must be a non empty list - - """ - if not isinstance(series, list): - raise ValueError("Input series is not valid, valid series - [2, 4, 8]") - if len(series) == 0: - raise ValueError("Input list must be a non empty list") - if not is_geometric_series(series): - raise ValueError("Input list is not a geometric series") - answer = 1 - for value in series: - answer *= value - return pow(answer, 1 / len(series)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From fd7bca3e86aa9576a79dbc18e361ab70d2b75ff9 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Tue, 2 Mar 2021 19:59:05 +0530 Subject: [PATCH 6/7] checks-3 --- maths/series/arithmetic_mean.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py index 2519a8a35c35..5b8c482a7452 100644 --- a/maths/series/arithmetic_mean.py +++ b/maths/series/arithmetic_mean.py @@ -1,3 +1,8 @@ +""" +ARITHMETIC MEAN : https://en.wikipedia.org/wiki/Arithmetic_mean +""" + + def is_arithmetic_series(series: list) -> bool: """ checking whether the input series is arithmetic series or not From 61aa8abf1b715c94a505603ea9084ee5e73d0b6e Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Tue, 2 Mar 2021 22:02:42 +0530 Subject: [PATCH 7/7] update-1 --- maths/series/arithmetic_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py index 5b8c482a7452..b5d64b63ac3f 100644 --- a/maths/series/arithmetic_mean.py +++ b/maths/series/arithmetic_mean.py @@ -1,5 +1,6 @@ """ ARITHMETIC MEAN : https://en.wikipedia.org/wiki/Arithmetic_mean + """ @@ -13,7 +14,6 @@ def is_arithmetic_series(series: list) -> bool: False >>> is_arithmetic_series([1, 2, 3]) True - """ if len(series) == 1: return True