Click4Ai

19.

Medium

Write a function standardize(arr) that standardizes an array to have zero mean and unit standard deviation (z-score normalization).

Formula: z = (value - mean) / std_dev

Example:

Input: arr = [10, 20, 30, 40, 50]

Output: [-1.4142, -0.7071, 0.0, 0.7071, 1.4142]

**Explanation:** Mean=30, Std=14.142. z = (x - 30) / 14.142

Constraints:

  • Array length: 2 <= len(arr) <= 1000
  • Array has non-zero standard deviation
  • Return a list of floats rounded to 4 decimal places
  • Test Cases

    Test Case 1
    Input: [10, 20, 30, 40, 50]
    Expected: [-1.4142, -0.7071, 0.0, 0.7071, 1.4142]
    Test Case 2
    Input: [1, 1, 3, 3]
    Expected: [-1.0, -1.0, 1.0, 1.0]
    Test Case 3
    Input: [0, 5, 10]
    Expected: [-1.2247, 0.0, 1.2247]
    + 2 hidden test cases