# pointStats.py

## References

### 2011

##############################################
# pointStat.py (v1.0) - Calculate some point statistics.
# Output: results in a hash {Average}{Min}{Max}{Median}{SquareSum}{StdDev}
# Assumptions: a non-empty numeric list input.

def pointStats( Numlist ):
"This Python function returns several well known population statistics."

# ideally should test if non-empty ...
cnt_val = len(Numlist)

sum_val = 0;
for val in Numlist:
sum_val += val
# sum_val = sum(Numlist)

avg_val = float(sum_val) / cnt_val

SortedNumlist = sorted (Numlist)
med_val = 0
if cnt_val % 2 == 1:
med_val = SortedNumlist[(cnt_val+1)/2-1]
else:
lower = SortedNumlist[cnt_val/2-1]
upper = SortedNumlist[cnt_val/2]
med_val = (float(lower + upper)) / 2

min_val = SortedNumlist[0]
max_val = SortedNumlist[cnt_val-1]

varS_val = 0
for val in Numlist:
varS_val += (avg_val - val) ** 2
stddev_val = (varS_val / cnt_val) ** 0.5

Result = {}
Result["Count"]       = cnt_val
Result["Sum"]         = sum_val
Result["Average"]     = avg_val
Result["Min"]         = min_val
Result["Max"]         = max_val
Result["Median"]      = med_val
Result["VarSquSum"]   = varS_val
Result["StdDev"]      = stddev_val

return Result
#return cnt_val, min_val, max_val, avg_val, med_val, stddev_val ;

# Call the pointStats function
NumList = [2, 20, 24, 19]
Result = pointStats( NumList )
print Result

# {'Count': 4, 'Min': 2, 'Max': 24, 'Sum': 65, 'Median': 19.5, 'VarSquSum': 284.75, 'StdDev': 8.4372685, 'Average': 16.25}