easy +8 pts

Z score normalization

Standardize a list of numbers into z-scores using the population standard deviation.

Implement the function `zscore_normalize(numbers)` that takes a non-empty list of numbers (ints or floats) and returns a new list of their z-scores. The z-score for each value x is calculated as: z = (x - mean) / std, where std is the population standard deviation (divide by n, not n-1). The mean and std are computed over the entire input list. If the standard deviation is zero, return a list of zeros of the same length as the input. Do not modify the input list. Round each z-score to one decimal place (e.g., -1.224744871391589 becomes -1.2, 0.0 stays 0.0).

Constraints

The input list has at least 1 element and at most 10000 elements. Values are integers or floats. You must compute using the population standard deviation. Your solution should run in O(n) time and O(n) extra space for the output.

Example

>>> zscore_normalize([1, 2, 3])
[-1.2, 0.0, 1.2]
>>> zscore_normalize([10])
[0.0]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Compute the mean as sum(numbers) / len(numbers).
Compute the population variance as the average of squared differences from the mean. Use a loop or sum for numerical stability.
Use math.sqrt to get the standard deviation from the variance.
Handle the edge case where the variance is zero (std = 0) by returning a list of zeros.
After computing each z-score, round it to one decimal place using round(z, 1) or a similar method.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.