-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add geometric_mean.py #4244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add geometric_mean.py #4244
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will fail with empty series. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also if series[0] is 0 will fail as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
for that I have check the size of list. If it is zero then It will raise an error. if len(series) == 0:
raise ValueError("Input list must be a non empty list") Would it be fine or ... how can I do it in a better way ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, didn't notice. |
||
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 *= _ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like it named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sorry for this silly mistake. |
||
return pow(answer, 1 / len(series)) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add link with some theory about algorithm.