Problem 203245 · medium · Level 02 Linear Data Structures

One Spread from Many Class Reports

variance · sample standard deviation · sum of squares · combining groups

Every class in a school reports only three numbers for a test: the number of students n, the class mean, and the class's sample standard deviation (dividing by n - 1; a class of one student reports 0.0). The head teacher wants the same three numbers for the whole school, as if all the marks had been put in one list, but the individual marks are no longer available.

Write combine(groups) where groups is a list of (n, mean, sd) tuples and return (total_n, overall_mean, overall_sd), where overall_sd is the sample standard deviation of all the marks together. If the school has only one student in total, overall_sd is None.

The setup provides school(k, seed), which returns k random classes as lists of marks, and class_report(marks), which returns a class's (n, mean, sd); use them to check your answer against a direct computation.

Examples

Input:  groups = [(3, 70.0, 10.0), (2, 70.0, 28.284271247461902)]
Output: (5, 70.0, 15.811388300841896)
Explanation: the classes were [60, 70, 80] and [50, 90]. Together: mean 70, squared
deviations 100 + 0 + 100 + 400 + 400 = 1000, variance 1000 / 4 = 250.

Input:  groups = [(2, 3.0, 1.4142135623730951), (1, 10.0, 0.0)]
Output: (3, 5.333333333333333, 4.163331998932266)
Explanation: the marks were [2, 4] and [10].

Constraints

  • 1 <= len(groups) <= 10**4; every n is at least 1, and the total is at most 10**7
  • means are between 0 and 100 and standard deviations between 0 and 60
  • floats are compared with a tolerance of 1e-6

Goals

  • Recover each group's sum of squared deviations from its reported standard deviation
  • Account for the gap between each group's mean and the overall mean
  • Combine summaries without seeing the raw data
Starting Python…