mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-02 17:31:08 +00:00
7d19d54f6f
* 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
29 lines
563 B
Python
29 lines
563 B
Python
from typing import List
|
|
|
|
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|