Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 5.py Basic Statistics
- class BasicStatistics:
- @staticmethod
- def sumOfAll(pCol):
- sum=0
- # iterable walk from 0 to length of pCol-1
- for idx in range(len(pCol)):
- currentValue = pCol[idx]
- sum += currentValue
- # for
- return sum
- # def sumOfAll
- @staticmethod
- def sum (pCol):
- sum = 0
- for n in pCol:
- sum += n
- # for
- return sum
- # def sum
- @staticmethod
- def average(pCol):
- try:
- return BasicStatistics.sum(pCol)/len(pCol)
- except Exception as e:
- return "Empty collection. Average not Computable."
- # def average
- @staticmethod
- def minValue(pCol):
- try:
- return min(pCol)
- except:
- return "Empty collection. Min not computable."
- # def minValue
- @staticmethod
- def maxValue(pCol):
- try:
- return max(pCol)
- except:
- return "Empty colletion. Max not computable."
- # def maxValue
- @staticmethod
- def variance(pCol):
- sumOfSquaredDistances = 0
- mean = BasicStatistics.average(pCol)
- bOKMean = type(mean)!=str
- if (bOKMean):
- for n in pCol:
- dist = n-mean
- distSquared = dist**2
- sumOfSquaredDistances += distSquared
- # for
- # if
- try:
- return sumOfSquaredDistances / len(pCol)
- except Exception as e:
- strMsg = f"Exception: {str(e)}\n"
- strMsg += f"Variance not computable.\n"
- return strMsg
- # try-except
- # def variance
- @staticmethod
- def standardDeviation(pCol):
- v = BasicStatistics.variance(pCol)
- bOKVariance = type(v)!=str
- if (bOKVariance):
- import math
- return math.sqrt(v)
- #return v**(1/2)
- # if
- # def standardDeviation
- # class BasicStatistics
- someNumbers = [10, 20, 30]
- theSum = BasicStatistics.sum(someNumbers)
- theAverage = BasicStatistics.average(someNumbers)
- theMin = BasicStatistics.minValue(someNumbers)
- theMax = BasicStatistics.maxValue(someNumbers)
- theVariance = BasicStatistics.variance(someNumbers)
- theStandardDevPop = BasicStatistics.standardDeviation(someNumbers)
- strMsg = f"Some stats about these numbers {someNumbers}\n"
- strMsg+=f"Sum: {theSum}\n"
- strMsg+=f"Average: {theAverage}\n"
- strMsg+=f"Min: {theMin}\n"
- strMsg+=f"Max: {theMax}\n"
- strMsg+=f"Variance: {theVariance}\n"
- strMsg+=f"Standard Deviation: {theStandardDevPop}\n"
- print(strMsg)
Advertisement
Add Comment
Please, Sign In to add comment