From ee9706d5ddeaf1cdb833cb4871b3ed349e41c191 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Sun, 28 Feb 2021 23:17:34 +0530 Subject: [PATCH 1/4] geometric_mean3 --- maths/series/geometric_mean.py | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 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..9f070474e0b0 --- /dev/null +++ b/maths/series/geometric_mean.py @@ -0,0 +1,55 @@ +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 + + """ + if len(series) == 1: + return True + common_ratio = series[1] / series[0] + for index in range(len(series) - 1): + if series[index + 1] / series[index] != common_ratio: + 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([1, 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 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 _ in series: + answer *= _ + return pow(answer, 1 / len(series)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From ffb7e8ed2171c5a9e8f0ec6b6758b3302edb60a2 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:00:20 +0530 Subject: [PATCH 2/4] update-GM.PY --- maths/series/geometric_mean.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/maths/series/geometric_mean.py b/maths/series/geometric_mean.py index 9f070474e0b0..50ae54ad6574 100644 --- a/maths/series/geometric_mean.py +++ b/maths/series/geometric_mean.py @@ -1,3 +1,8 @@ +""" +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 @@ -8,14 +13,19 @@ def is_geometric_series(series: list) -> bool: True >>> is_geometric_series([1, 2, 3]) False + >>> is_geometric_series([0, 0, 3]) + False """ if len(series) == 1: return True - common_ratio = series[1] / series[0] - for index in range(len(series) - 1): - if series[index + 1] / series[index] != common_ratio: - return False + 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 @@ -29,23 +39,33 @@ def geometric_mean(series: list) -> float: 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 _ in series: - answer *= _ + for value in series: + answer *= value return pow(answer, 1 / len(series)) From 11792ec9747bec5d00e51edcd462bf87150cdba9 Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:46:00 +0530 Subject: [PATCH 3/4] update-AM.PY --- maths/series/arithmetic_mean.py | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 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..2519a8a35c35 --- /dev/null +++ b/maths/series/arithmetic_mean.py @@ -0,0 +1,61 @@ +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 arithmetic mean of series + + >>> arithmetic_mean([2, 4, 6]) + 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): + ... + 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 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 val in series: + answer += val + return answer / len(series) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 79cb3b2a011156007f249f3c3ac254b7b8e7f07c Mon Sep 17 00:00:00 2001 From: ayushbisht2001 <61404154+ayushbisht2001@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:48:09 +0530 Subject: [PATCH 4/4] Revert "update-AM.PY" This reverts commit 11792ec9747bec5d00e51edcd462bf87150cdba9. --- maths/series/arithmetic_mean.py | 61 --------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 maths/series/arithmetic_mean.py diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py deleted file mode 100644 index 2519a8a35c35..000000000000 --- a/maths/series/arithmetic_mean.py +++ /dev/null @@ -1,61 +0,0 @@ -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 arithmetic mean of series - - >>> arithmetic_mean([2, 4, 6]) - 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): - ... - 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 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 val in series: - answer += val - return answer / len(series) - - -if __name__ == "__main__": - import doctest - - doctest.testmod()