Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #mylist.py
- class MyList:
- #dunder double underscore
- BATATA = "batata frita"
- #initializer of instances of the MyList datatype
- def __init__(
- self,
- pValues = None
- ):
- if (pValues is None):
- self.mList = []
- else:
- self.mList = pValues
- #def __init__
- #how instances of type MyList should be
- #represented as string
- def __str__(self):
- strAll = ""
- strAll+=self.mList.__str__()
- return strAll
- #def __str__
- #dynamic
- def min (self):
- theSmallest = None
- for v in self.mList:
- if (theSmallest is None):
- theSmallest = v
- else:
- if (v<theSmallest):
- theSmallest = v
- #if
- #else
- #for
- return theSmallest
- #def min
- def max(self):
- theGreatest = None
- for v in self.mList:
- if (theGreatest is None):
- theGreatest = v
- else:
- if(v>theGreatest):
- theGreatest = v
- #if
- #else
- #for
- return theGreatest
- #def max
- def howManyValues(self):
- return len(self.mList)
- #def howManyValues
- #mean / average / média
- def mean (self):
- howMany = self.howManyValues()
- if (howMany>0):
- theSum = 0
- for v in self.mList:
- #theSum = theSum+v
- #cumulative sum +=
- theSum+=v
- #for
- theMean = theSum / howMany
- return theMean
- else:
- return None
- #def mean
- """
- p1 p2 p3 ... pn
- pM
- sum = (p1-pM)^2 + (p2-pM)^2 + (p3-pM)^2 ... (pn-pM)^2
- sum/howMany
- """
- def variance(self):
- howMany = self.howManyValues()
- theMean = self.mean()
- theSum = 0
- if (howMany>0 and (theMean!=None)):
- for v in self.mList:
- diff = v-theMean
- diffSquared = diff**2
- theSum += diffSquared
- #for
- theVariance = theSum / howMany
- return theVariance
- else:
- return None
- #def variance
- #stdevp = sqrt(variance)
- #stdevp = variance ** (1/2)
- def standardDeviation(self):
- if (self.howManyValues()>0):
- #return self.variance()**(1/2)
- variance = self.variance()
- squareRoot = variance**(1/2)
- return squareRoot
- else:
- return None
- #else
- #def standardDeviation
- """
- example:
- working on list [10, 20, 40, 40]
- set([10, 20, 40, 40]) --> [10, 20, 40]
- working over a collection without repetitions...
- set
- the return should be this dictionary:
- {10:1, 20:1, 40:2}
- """
- def absoluteFrequencyOfElements(self):
- retDict = {}
- if (self.howManyValues()>0):
- setWithoutRepitions = set(self.mList) #[10, 20, 40] #optimization => does not question repeated elements
- #for v in self.mList: #[10, 20, 40, 40] #would count 40 twice
- for v in setWithoutRepitions:
- iCount = self.count(v)
- retDict[v] = iCount
- #retDict[v] = self.count(v)
- #for
- return retDict
- else:
- return None
- #def absoluteFrequencyOfElements
- def count(self, pEl):
- bShouldWork = self.howManyValues() and \
- pEl!=None
- if (bShouldWork):
- iCounter = 0
- for v in self.mList:
- if (pEl==v):
- iCounter+=1
- #if
- #for
- return iCounter
- else:
- return 0
- #def count
- def mostFrequentValues (self):
- #dictionary of absolute frequencies
- daf = self.absoluteFrequencyOfElements()
- if (daf!=None):
- theKeys = daf.keys()
- theGreatest = None
- for k in theKeys:
- v = daf[k]
- if (theGreatest is None):
- theGreatest = v
- else:
- if (v>theGreatest):
- theGreatest = v
- #if
- #else
- #for
- #what do I have?????
- #in var theGreatest we capture the most
- #frequent value in the collection (self.mList)
- listMode = []
- for k in theKeys:
- if (daf[k]==theGreatest):
- listMode.append(k)
- #if
- #for
- return listMode
- else:
- return None
- #def mostFrequentValues
- #TPC: leastFrequentValues
- def mode(self):
- return self.mostFrequentValues()
- #def
- #class MyList
- l1 = MyList()
- l2 = MyList([10, 20, 30])
- smallestInL1 = l1.min()
- greatestInL2 = l2.max()
- print ("l1: ", l1)
- print ("l2: ", l2)
- print ("smallest in l1: ", smallestInL1)
- print ("greatest in l2: ", greatestInL2)
- print ("How many elements in l1: ",
- l1.howManyValues()
- )
- print ("How many elements in l2: ",
- l2.howManyValues()
- )
- theMeanL1 = l1.mean()
- theMeanL2 = l2.mean()
- print ("The mean of l1: ", theMeanL1)
- print ("The mean of l2: ", theMeanL2)
- l3 = MyList([20, 20, 40, 40, 50, 50, 50])
- v3 = l3.variance()
- dp3 = l3.standardDeviation()
- print("The variance of {} is {}".format(l3, v3))
- print("The standard deviation of {} is {}".\
- format(l3, dp3))
- daf = l3.absoluteFrequencyOfElements()
- print (daf)
- theModeList = l3.mode()
- print ("The mode (as a list) for {} is {}".\
- format(l3, theModeList))
- l2 = MyList()
- l44 = MyList([44, 44, 44])
- l2.mode()
- l44.mode()
- #the left-operand is NOT an instance of the class
- #but the class's name
- print(MyList.BATATA) #static
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement