easy +10 pts

Array mean and std

Compute the mean and population standard deviation of a list of numbers.

Write a function `array_stats(nums)` that takes a list of numbers (integers or floats) and returns a tuple `(mean, std)` where: - `mean` is the arithmetic average of the numbers, rounded to 3 decimal places (use Python's round, which rounds half to even). - `std` is the population standard deviation (divide by n, not n-1), also rounded to 3 decimal places. If the list is empty, return `(0.0, 0.0)`. The function must always return floats even if the input contains only integers. For example, `array_stats([2, 4, 4, 4, 5, 5, 7, 9])` should return `(5.0, 2.0)`. Note: the population standard deviation is the square root of the average of squared deviations from the mean.

Constraints

The input list length is between 0 and 10^5. Numbers are finite real numbers. The result will fit within Python's floating-point precision. The time complexity should be O(n).

Example

>>> array_stats([2, 4, 4, 4, 5, 5, 7, 9])
(5.0, 2.0)
>>> array_stats([10, 20, 30])
(20.0, 8.165)
>>> array_stats([])
(0.0, 0.0)
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Calculate the mean first, then compute the average of squared differences from that mean.
Use math.sqrt for the square root, but remember to round to 3 decimals only at the end.
For an empty list, detect it early and return (0.0, 0.0).
The population standard deviation divides by n, not n-1.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.