Consider X as the  aleatory  variable that count the number of letters in a word. Write a function that, give in input an array of words (strings), calculate the  variance  of X.
Max decimal of the variance : 4.

Some wiki: Variance ,
 Aleatory variable 

Example:

Consider "Hello" and "World":

X is { 5 } with P(X = 5) = 1 beacuse the two words has the same length.  
So E[X] = 5 x 1 = 5 and the standard formula for variance is E[(X - u)^2] so  1 x (5-5)^2 = 0
or you can calculate with the other formula E[X^2] - E[X]^2 = 5^2 x 1 - 5^2 = 0

Consider "Hi" and "World":

X is { 2, 5 } with P(X = 5) = 1/2 and P(X = 2) = 1/2.  
So E[X] = 5 x 1/2 + 2 x 1/2 = 3.5 and the standard formula for variance is E[(X - u)^2] so  1/2 x (2-3.5)^2 + 1/2 x (5 - 3.5)^2 = 2.25
or you can calculate with the other formula E[X^2] - E[X]^2 = (5^2 x 1/2 + 2^2 x 1/2) - 3.5^2 = 2.25

Source: codewars | Difficulty: EASY
Tags: Mathematics

Starter code: def variance(words):
	

Solutions:
variance = lambda w: round(__import__('statistics').pvariance(map(len, w)), 4)


from statistics import pvariance

def variance(words):
	return round(pvariance(map(len, words)), 4)


from statistics import pvariance as var
variance = lambda words: round(var((len(word) for word in words)), 4)


def variance(words):
	from statistics import pvariance as var
	return round(var((len(word) for word in words)), 4)


def mean(x):
	s = 0
	for i in range(len(x)):
		s = s + x[i]
	return s / len(x)

def variance(words):
	x = [len(word) for word in words]
	return round(mean([num ** 2 for num in x]) - mean(x) ** 2, 4)


def variance(li):
	common = 1 / len(li)
	e_of_x = sum((len(i) * common for i in li))
	result = sum((len(i) ** 2 * common for i in li)) - e_of_x ** 2
	return round(result, 4)


def variance(array):
	nums = list(map(len, array))
	length = float(len(nums))
	average = sum(nums) / length
	return round(sum(((average - a) ** 2 for a in nums)) / length, 4)


from collections import Counter
from numpy import average

def variance(words):
	(values, weights) = zip(*Counter(map(len, words)).items())
	return round(average((values - average(values, weights=weights)) ** 2, weights=weights), 4)