-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution.py
More file actions
35 lines (28 loc) · 842 Bytes
/
solution.py
File metadata and controls
35 lines (28 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
SUBMIT = True
def list_average(_numbers: list[float]) -> float:
"""Returns the mean of a numeric list.
Example usage:
>>> list_average([1, 2, 3, 4, 5])
2.5
>>> list_average([10, 20, 30])
20.0
"""
sum=0
for i in _numbers:
sum+=i
return sum/len(_numbers)
def test() -> None:
"""Simple self-test for Computing Average."""
cases = [([1, 2, 3, 4, 5], 3.0), ([10, 20, 30], 20.0), ([5], 5.0)]
for nums, expected in cases:
try:
res = list_average([float(x) for x in nums])
assert abs(res - expected) < 1e-9, (
f"Failed for {nums}: expected {expected}, got {res}"
)
except AssertionError as e:
print(f"❌ {e}")
return
print("✅ All tests passed!")
if __name__ == "__main__":
test()