2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2019-07-01 08:10:18 +00:00
|
|
|
|
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def mean(nums: list) -> float:
|
2019-10-21 20:36:33 +00:00
|
|
|
"""
|
2021-06-16 06:33:23 +00:00
|
|
|
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
|
2019-10-21 20:36:33 +00:00
|
|
|
"""
|
2021-06-16 06:33:23 +00:00
|
|
|
if not nums:
|
|
|
|
raise ValueError("List is empty")
|
|
|
|
return sum(nums) / len(nums)
|
2019-07-01 08:10:18 +00:00
|
|
|
|
2019-01-19 20:49:06 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2021-06-16 06:33:23 +00:00
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|