BasicsNumPySummary StatsGoogleMetaAmazon
Problem
Given a 1D list or NumPy array of numbers x, implement mean_and_variance(x) returning a tuple (mean, variance) where variance is the population variance (N1∑(xi−μ)2).
Why It Matters: Computing exact mean and variance is the foundation of feature scaling, Gaussian Naive Bayes, PCA, and Batch Normalization.
Sample Inputs & Expected Outputs
Example 1 — Standard list
>>> mean_and_variance([2, 4, 4, 4, 5, 5, 7, 9])
(5.0, 4.0)
Mean is 5.0; average squared deviation from 5.0 is 4.0.
Example 2 — Constant values
>>> mean_and_variance([7, 7, 7])
(7.0, 0.0)
All values equal the mean, so variance is 0.0.
Example 3 — Negative & positive values
>>> mean_and_variance([-2, 0, 2])
(0.0, 2.6667)
Mean is 0.0; ((4 + 0 + 4) / 3) = 2.6667.
Constraints & Edge Cases
- Return
(mean, variance)as Python floats. - Input list has at least 1 number.
Constraints & Evaluation Criteria
- Return `(mean, variance)` as Python floats.
- Input list has at least 1 number.
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[2, 4, 4, 4, 5, 5, 7, 9]
Expected Output / Criteria
(5.0, 4.0)