Problem 442779 · easy · Level 04 Non-Linear Data Structures

Only the Digits You Can Trust

Monte Carlo · standard error · sample standard deviation · significant figures

A student's simulation prints "the probability is 0.33334". With 100,000 trials, only the first three decimals of that number mean anything. Write a helper that reports a simulation's result honestly.

results is the list of outcomes of the independent trials of a simulation: numbers, or True/False for "did the event happen" (which count as 1 and 0). Write honest_report(results) that returns a tuple (estimate, se, text):

  • estimate is the mean of the results,
  • se is its standard error, s / sqrt(n), where s is the sample standard deviation of the results (dividing by n - 1),
  • text is "<estimate> +/- <se>", where both numbers are formatted with the same number of decimals d: the position of the first significant digit of se. For se = 0.0773 that is d = 2, for se = 0.0015 it is d = 3, and for se >= 1 it is d = 0 (whole numbers). Format with exactly d decimals, keeping trailing zeros, as an f-string such as f"{x:.2f}" does.

Examples

Input:  results = [1, 0, 0, 1, 1, 0, 1, 0]
Output: (0.5, 0.1889822365046136, "0.5 +/- 0.2")
Explanation: the sample standard deviation is 0.5345, and 0.5345 / sqrt(8) = 0.189,
whose first significant digit is in the first decimal place.

Input:  results = [1200, 800, 3000, 150, 99]
Output: (1049.8, 529.2400589524568, "1050 +/- 529")

Constraints

  • 2 <= len(results) <= 10**5, and not all results are equal (so se > 0)
  • floats are compared with a tolerance of 1e-6; the text must match exactly

Goals

  • Treat a simulation's result as a sample mean with a standard error
  • Compute the standard error s / sqrt(n) from the simulated outcomes
  • Report an estimate with no more decimals than its standard error supports
Starting Python…