Putting a Number on the Tail

Distributions & ShapeMedium+20 XP+20

Putting a Number on the Tail

Saying a distribution "has a long right tail" is useful, but two datasets can both lean right by very different amounts. The skewness coefficient turns the lean into a single number. For values x1,…,xnx_1, \dots, x_nx1​,…,xn​ with mean xˉ\bar{x}xˉ:

g=1n∑i=1n(xi−xˉ)3(1n∑i=1n(xi−xˉ)2)3/2g = \frac{\frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^3}{\left(\frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2\right)^{3/2}}g=(n1​∑i=1n​(xi​−xˉ)2)3/2n1​∑i=1n​(xi​−xˉ)3​
  • The numerator cubes each distance from the mean. Cubing keeps the sign, so a value far above the mean pushes ggg up and a value far below pushes it down, and it lets the farthest values dominate.
  • The denominator is the population standard deviation σ\sigmaσ raised to the power 333. That makes ggg unit-free: the same data recorded in seconds instead of minutes gives the same ggg.

Task: write skewness(values) that returns a tuple (g, tail):

  • g is the coefficient above, rounded to 4 decimal places.
  • tail names the direction of the longer tail, decided from the rounded g: "right" if g > 0, "left" if g < 0, and "symmetric" if g is exactly 0.
  • If every value is identical there is no spread to divide by: return (0.0, "symmetric").

values is a non-empty list of numbers in any order.

Examples

values = [12, 14, 15, 15, 16, 18, 41]
returns (1.8998, 'right')
values = [2, 4, 6, 8, 10]
returns (0.0, 'symmetric')

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run