CBuilderLab· about 40 min· in the browser
Lab: mean, median and standard deviation by hand
Implement the three basic measures yourself, without libraries, and understand why the median withstands outliers.
Teaches: Statistics — mean, median and spread
Requires: Python — lists, loops and dictionaries
Theory
Mean = sum/count. Median = the middle value after sorting (the mean of the two middle values for an even count). Standard deviation (population) = the square root of the mean of the squared deviations.
Sub-tasks
- medel (mean), median — Implement
medelandmedian. - std — Implement the population standard deviation
std.
The starter code
runs in your browserdef medel(x):
# TODO
...
def median(x):
# TODO: sortera först; jämnt antal → medel av de två mittersta
...
def std(x):
# TODO: populationsstandardavvikelse: sqrt(medel av (xi - medel)^2)
...
You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.
Try the diagnosticCreate a free accountExpected results
median([7,2,9,4,11]) → 7; median([1,2,3,4]) → 2.5; std([2,4,4,4,5,5,7,9]) → 2.0.
Common mistakes
- Forgets to sort before the median.
- Uses the sample variance (n−1) — here it should be n.
- Mixes up variance and standard deviation (the square root is missing).