Return the average absolute deviation of a list of numbers. Wiki: https://en.wikipedia.org/wiki/Average_absolute_deviation >>> average_absolute_deviation([0]) 0.0 >>> average_absolute_deviation([4, 1, 3, 2]) 1.0 >>> average_absolute_deviation([2, 70, 6, 50, 20, 8, 4, 0]
(nums: list[int])
| 1 | def average_absolute_deviation(nums: list[int]) -> float: |
| 2 | """ |
| 3 | Return the average absolute deviation of a list of numbers. |
| 4 | Wiki: https://en.wikipedia.org/wiki/Average_absolute_deviation |
| 5 | |
| 6 | >>> average_absolute_deviation([0]) |
| 7 | 0.0 |
| 8 | >>> average_absolute_deviation([4, 1, 3, 2]) |
| 9 | 1.0 |
| 10 | >>> average_absolute_deviation([2, 70, 6, 50, 20, 8, 4, 0]) |
| 11 | 20.0 |
| 12 | >>> average_absolute_deviation([-20, 0, 30, 15]) |
| 13 | 16.25 |
| 14 | >>> average_absolute_deviation([]) |
| 15 | Traceback (most recent call last): |
| 16 | ... |
| 17 | ValueError: List is empty |
| 18 | """ |
| 19 | if not nums: # Makes sure that the list is not empty |
| 20 | raise ValueError("List is empty") |
| 21 | |
| 22 | average = sum(nums) / len(nums) # Calculate the average |
| 23 | return sum(abs(x - average) for x in nums) / len(nums) |
| 24 | |
| 25 | |
| 26 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected