From 2855da95beacd623d4368a7b69ee206864b1a6f5 Mon Sep 17 00:00:00 2001 From: atharva01903 Date: Tue, 5 Oct 2021 22:30:40 +0530 Subject: [PATCH 1/4] added harmonic mean --- maths/series/harmonic_mean.py | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 maths/series/harmonic_mean.py diff --git a/maths/series/harmonic_mean.py b/maths/series/harmonic_mean.py new file mode 100644 index 000000000000..a9df90809c7a --- /dev/null +++ b/maths/series/harmonic_mean.py @@ -0,0 +1,40 @@ +""" +HARMONIC MEAN : https://en.wikipedia.org/wiki/Harmonic_mean + +""" + + +def harmonic_mean(series: list) -> float: + """ + return the harmonic mean of series + + >>> harmonic_mean([1, 4, 4]) + 2.0 + >>> harmonic_mean([3, 6, 9, 12]) + 5.759999999999999 + >>> harmonic_mean(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 6] + >>> harmonic_mean([1, 2, 3]) + 1.6363636363636365 + >>> harmonic_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") + answer = 0 + for val in series: + answer += 1 / val + return len(series) / answer + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 43af1577fed197182ee3fddb3a4927d6ae2f04b7 Mon Sep 17 00:00:00 2001 From: Atharva Deshpande Date: Thu, 7 Oct 2021 21:22:13 +0530 Subject: [PATCH 2/4] Update maths/series/harmonic_mean.py Updated the write-up of reference given in the code. Co-authored-by: John Law --- maths/series/harmonic_mean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/series/harmonic_mean.py b/maths/series/harmonic_mean.py index a9df90809c7a..1f759e424b33 100644 --- a/maths/series/harmonic_mean.py +++ b/maths/series/harmonic_mean.py @@ -1,6 +1,6 @@ """ -HARMONIC MEAN : https://en.wikipedia.org/wiki/Harmonic_mean - +Harmonic mean +Reference: https://en.wikipedia.org/wiki/Harmonic_mean """ From a0e996c6c402637990903395f1d91377d68738bb Mon Sep 17 00:00:00 2001 From: atharva01903 Date: Tue, 12 Oct 2021 20:12:27 +0530 Subject: [PATCH 3/4] changes in arithmetic and geometric mean code --- maths/series/arithmetic_mean.py | 30 +++---------------------- maths/series/geometric_mean.py | 39 ++++----------------------------- 2 files changed, 7 insertions(+), 62 deletions(-) diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py index b5d64b63ac3f..8101013ec451 100644 --- a/maths/series/arithmetic_mean.py +++ b/maths/series/arithmetic_mean.py @@ -1,29 +1,9 @@ """ -ARITHMETIC MEAN : https://en.wikipedia.org/wiki/Arithmetic_mean - +Arithmetic mean +Reference: https://en.wikipedia.org/wiki/Arithmetic_mean """ -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 @@ -37,9 +17,7 @@ def arithmetic_mean(series: list) -> float: ... 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 + 4.333333333333333 >>> arithmetic_mean([1, 2, 3]) 2.0 >>> arithmetic_mean([]) @@ -52,8 +30,6 @@ def arithmetic_mean(series: list) -> float: 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 diff --git a/maths/series/geometric_mean.py b/maths/series/geometric_mean.py index 50ae54ad6574..9b58a41ac3de 100644 --- a/maths/series/geometric_mean.py +++ b/maths/series/geometric_mean.py @@ -1,34 +1,9 @@ """ -GEOMETRIC MEAN : https://en.wikipedia.org/wiki/Geometric_mean +Geometric Mean +Reference : 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 @@ -44,13 +19,9 @@ def geometric_mean(series: list) -> float: ... 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 + 1.8171205928321397 >>> geometric_mean([0, 2, 3]) - Traceback (most recent call last): - ... - ValueError: Input list is not a geometric series + 0.0 >>> geometric_mean([]) Traceback (most recent call last): ... @@ -61,8 +32,6 @@ def geometric_mean(series: list) -> float: 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 From d039f7f308d72cd8ce179fee426b1ea9a0a85915 Mon Sep 17 00:00:00 2001 From: atharva01903 Date: Sun, 17 Oct 2021 14:07:37 +0530 Subject: [PATCH 4/4] mean and series added in a single file --- maths/series/arithmetic.py | 77 +++++++++++++++++++++++++++ maths/series/arithmetic_mean.py | 42 --------------- maths/series/geometric.py | 83 +++++++++++++++++++++++++++++ maths/series/geometric_mean.py | 44 ---------------- maths/series/harmonic.py | 92 +++++++++++++++++++++++++++++++++ maths/series/harmonic_mean.py | 40 -------------- 6 files changed, 252 insertions(+), 126 deletions(-) create mode 100644 maths/series/arithmetic.py delete mode 100644 maths/series/arithmetic_mean.py create mode 100644 maths/series/geometric.py delete mode 100644 maths/series/geometric_mean.py create mode 100644 maths/series/harmonic.py delete mode 100644 maths/series/harmonic_mean.py diff --git a/maths/series/arithmetic.py b/maths/series/arithmetic.py new file mode 100644 index 000000000000..dc28c5c7bc5f --- /dev/null +++ b/maths/series/arithmetic.py @@ -0,0 +1,77 @@ +""" +Arithmetic mean +Reference: https://en.wikipedia.org/wiki/Arithmetic_mean + +Arithmetic series +Reference: https://en.wikipedia.org/wiki/Arithmetic_series +(The URL above will redirect you to arithmetic progression) +""" + + +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 + >>> is_arithmetic_series(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 6] + >>> is_arithmetic_series([]) + 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 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]) + 4.333333333333333 + >>> 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") + answer = 0 + for val in series: + answer += val + return answer / len(series) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/series/arithmetic_mean.py b/maths/series/arithmetic_mean.py deleted file mode 100644 index 8101013ec451..000000000000 --- a/maths/series/arithmetic_mean.py +++ /dev/null @@ -1,42 +0,0 @@ -""" -Arithmetic mean -Reference: https://en.wikipedia.org/wiki/Arithmetic_mean -""" - - -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]) - 4.333333333333333 - >>> 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") - answer = 0 - for val in series: - answer += val - return answer / len(series) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/maths/series/geometric.py b/maths/series/geometric.py new file mode 100644 index 000000000000..7b6239b1585d --- /dev/null +++ b/maths/series/geometric.py @@ -0,0 +1,83 @@ +""" +Geometric Mean +Reference : https://en.wikipedia.org/wiki/Geometric_mean + +Geometric series +Reference: https://en.wikipedia.org/wiki/Geometric_series +""" + + +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 + >>> is_geometric_series([]) + Traceback (most recent call last): + ... + ValueError: Input list must be a non empty list + >>> is_geometric_series(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 8] + """ + 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 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]) + 1.8171205928321397 + >>> geometric_mean([0, 2, 3]) + 0.0 + >>> 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") + answer = 1 + for value in series: + answer *= value + return pow(answer, 1 / len(series)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/series/geometric_mean.py b/maths/series/geometric_mean.py deleted file mode 100644 index 9b58a41ac3de..000000000000 --- a/maths/series/geometric_mean.py +++ /dev/null @@ -1,44 +0,0 @@ -""" -Geometric Mean -Reference : https://en.wikipedia.org/wiki/Geometric_mean -""" - - -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]) - 1.8171205928321397 - >>> geometric_mean([0, 2, 3]) - 0.0 - >>> 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") - answer = 1 - for value in series: - answer *= value - return pow(answer, 1 / len(series)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/maths/series/harmonic.py b/maths/series/harmonic.py new file mode 100644 index 000000000000..50f29c93dd5f --- /dev/null +++ b/maths/series/harmonic.py @@ -0,0 +1,92 @@ +""" +Harmonic mean +Reference: https://en.wikipedia.org/wiki/Harmonic_mean + +Harmonic series +Reference: https://en.wikipedia.org/wiki/Harmonic_series(mathematics) +""" + + +def is_harmonic_series(series: list) -> bool: + """ + checking whether the input series is arithmetic series or not + >>> is_harmonic_series([ 1, 2/3, 1/2, 2/5, 1/3]) + True + >>> is_harmonic_series([ 1, 2/3, 2/5, 1/3]) + False + >>> is_harmonic_series([1, 2, 3]) + False + >>> is_harmonic_series([1/2, 1/3, 1/4]) + True + >>> is_harmonic_series([2/5, 2/10, 2/15, 2/20, 2/25]) + True + >>> is_harmonic_series(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [1, 2/3, 2] + >>> is_harmonic_series([]) + Traceback (most recent call last): + ... + ValueError: Input list must be a non empty list + >>> is_harmonic_series([0]) + Traceback (most recent call last): + ... + ValueError: Input series cannot have 0 as an element + >>> is_harmonic_series([1,2,0,6]) + Traceback (most recent call last): + ... + ValueError: Input series cannot have 0 as an element + """ + if not isinstance(series, list): + raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]") + if len(series) == 0: + raise ValueError("Input list must be a non empty list") + if len(series) == 1 and series[0] != 0: + return True + rec_series = [] + series_len = len(series) + for i in range(0, series_len): + if series[i] == 0: + raise ValueError("Input series cannot have 0 as an element") + rec_series.append(1 / series[i]) + common_diff = rec_series[1] - rec_series[0] + for index in range(2, series_len): + if rec_series[index] - rec_series[index - 1] != common_diff: + return False + return True + + +def harmonic_mean(series: list) -> float: + """ + return the harmonic mean of series + + >>> harmonic_mean([1, 4, 4]) + 2.0 + >>> harmonic_mean([3, 6, 9, 12]) + 5.759999999999999 + >>> harmonic_mean(4) + Traceback (most recent call last): + ... + ValueError: Input series is not valid, valid series - [2, 4, 6] + >>> harmonic_mean([1, 2, 3]) + 1.6363636363636365 + >>> harmonic_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") + answer = 0 + for val in series: + answer += 1 / val + return len(series) / answer + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/maths/series/harmonic_mean.py b/maths/series/harmonic_mean.py deleted file mode 100644 index 1f759e424b33..000000000000 --- a/maths/series/harmonic_mean.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -Harmonic mean -Reference: https://en.wikipedia.org/wiki/Harmonic_mean -""" - - -def harmonic_mean(series: list) -> float: - """ - return the harmonic mean of series - - >>> harmonic_mean([1, 4, 4]) - 2.0 - >>> harmonic_mean([3, 6, 9, 12]) - 5.759999999999999 - >>> harmonic_mean(4) - Traceback (most recent call last): - ... - ValueError: Input series is not valid, valid series - [2, 4, 6] - >>> harmonic_mean([1, 2, 3]) - 1.6363636363636365 - >>> harmonic_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") - answer = 0 - for val in series: - answer += 1 / val - return len(series) / answer - - -if __name__ == "__main__": - import doctest - - doctest.testmod()