v9.1.1
Menu

Array Statistics

easyPROArray MathematicsNumPy StatisticsNumpy

Problem Statement

Calculate basic statistics of a NumPy array.

Background

NumPy provides statistical functions:

  • np.sum(): Sum of elements
  • np.mean(): Average
  • np.min(), np.max(): Min and max
  • np.std(): Standard deviation

Your Task

Write a function array_stats(arr) that returns a dictionary with:

  • "sum": Sum of all elements
  • "mean": Mean (average), rounded to 2 decimals
  • "min": Minimum value
  • "max": Maximum value
  • "std": Population standard deviation (divide by nn, not n−1n-1), rounded to 2 decimals

Output Format

Return a dictionary with exactly these five keys.

Example

Input
[1, 2, 3, 4, 5]
Output
{'sum': 15, 'mean': 3.0, 'min': 1, 'max': 5, 'std': 1.41}
Reasoning
  • Input: arr is a 1-D array of five integers, x=(1,2,3,4,5)x = (1, 2, 3, 4, 5), so n=5n = 5.
  • Sum and mean: ∑ixi=1+2+3+4+5=15\sum_i x_i = 1+2+3+4+5 = 15, and the mean is μ=15/5=3\mu = 15 / 5 = 3, shown as 3.0 (a decimal, since averages are generally not whole).
  • Min and max: the smallest and largest entries are 1 and 5; like the sum, they stay whole numbers because every input is an integer.
  • Standard deviation: the deviations from the mean are −2,−1,0,1,2-2, -1, 0, 1, 2, whose squares are 4,1,0,1,44, 1, 0, 1, 4 (total 1010). Dividing by n=5n = 5 gives a variance of 22, so σ=2≈1.4142\sigma = \sqrt{2} \approx 1.4142, which rounds to 1.41 at 2 decimal places.
  • Output: a dictionary with exactly the keys sum, mean, min, max, std.
  • Notice that dividing by n−1=4n - 1 = 4 instead would give 2.5≈1.58\sqrt{2.5} \approx 1.58, which is not the expected answer. A constant array such as (5,5,5,5)(5, 5, 5, 5) has every deviation zero, so its std is 0.0.

Constraints

  • Round mean and std to 2 decimal places
  • Array will have at least 1 element

Learn this in

More from Numpy

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.