Average mean refactor (#4485)

* Average mean refactor

Added doctests and type hints to average_mean

* Wiki link added

* Empty list check added

Empty list check added
Type hint changed to typing.List
This commit is contained in:
Grigoriy Hanin 2021-06-16 09:33:23 +03:00 committed by GitHub
parent 0eabdb54b1
commit 7d19d54f6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,20 +1,28 @@
"""Find mean of a list of numbers."""
from typing import List
def average(nums):
"""Find mean of a list of numbers."""
def mean(nums: List) -> float:
"""
Find mean of a list of numbers.
Wiki: https://en.wikipedia.org/wiki/Mean
>>> mean([3, 6, 9, 12, 15, 18, 21])
12.0
>>> mean([5, 10, 15, 20, 25, 30, 35])
20.0
>>> mean([1, 2, 3, 4, 5, 6, 7, 8])
4.5
>>> mean([])
Traceback (most recent call last):
...
ValueError: List is empty
"""
if not nums:
raise ValueError("List is empty")
return sum(nums) / len(nums)
def test_average():
"""
>>> test_average()
"""
assert 12.0 == average([3, 6, 9, 12, 15, 18, 21])
assert 20 == average([5, 10, 15, 20, 25, 30, 35])
assert 4.5 == average([1, 2, 3, 4, 5, 6, 7, 8])
if __name__ == "__main__":
"""Call average module to find mean of a specific list of numbers."""
print(average([2, 4, 6, 8, 20, 50, 70]))
import doctest
doctest.testmod()