am_dot_com

FP 2021-11-29

Nov 29th, 2021 (edited)
639
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #mylist.py
  2.  
  3. class MyList:
  4.     #dunder double underscore
  5.  
  6.     BATATA = "batata frita"
  7.  
  8.     #initializer of instances of the MyList datatype
  9.     def __init__(
  10.         self,
  11.         pValues = None
  12.     ):
  13.         if (pValues is None):
  14.             self.mList = []
  15.         else:
  16.             self.mList = pValues
  17.     #def __init__
  18.  
  19.     #how instances of type MyList should be
  20.     #represented as string
  21.     def __str__(self):
  22.         strAll = ""
  23.         strAll+=self.mList.__str__()
  24.         return strAll
  25.     #def __str__
  26.  
  27.     #dynamic
  28.     def min (self):
  29.         theSmallest = None
  30.  
  31.         for v in self.mList:
  32.             if (theSmallest is None):
  33.                 theSmallest = v
  34.             else:
  35.                 if (v<theSmallest):
  36.                     theSmallest = v
  37.                 #if
  38.             #else
  39.         #for
  40.         return theSmallest
  41.     #def min
  42.  
  43.     def max(self):
  44.         theGreatest = None
  45.         for v in self.mList:
  46.             if (theGreatest is None):
  47.                 theGreatest = v
  48.             else:
  49.                 if(v>theGreatest):
  50.                     theGreatest = v
  51.                 #if
  52.             #else
  53.         #for
  54.         return theGreatest
  55.     #def max
  56.  
  57.     def howManyValues(self):
  58.         return len(self.mList)
  59.     #def howManyValues
  60.  
  61.     #mean / average / média
  62.     def mean (self):
  63.         howMany = self.howManyValues()
  64.         if (howMany>0):
  65.             theSum = 0
  66.             for v in self.mList:
  67.                 #theSum = theSum+v
  68.                 #cumulative sum +=
  69.                 theSum+=v
  70.             #for
  71.             theMean = theSum / howMany
  72.             return theMean
  73.         else:
  74.             return None
  75.     #def mean
  76.  
  77.     """
  78.    p1 p2 p3 ... pn
  79.    pM
  80.    
  81.    sum = (p1-pM)^2 + (p2-pM)^2 + (p3-pM)^2 ... (pn-pM)^2
  82.    sum/howMany
  83.    """
  84.     def variance(self):
  85.         howMany = self.howManyValues()
  86.         theMean = self.mean()
  87.         theSum = 0
  88.         if (howMany>0 and (theMean!=None)):
  89.             for v in self.mList:
  90.                 diff = v-theMean
  91.                 diffSquared = diff**2
  92.                 theSum += diffSquared
  93.             #for
  94.             theVariance = theSum / howMany
  95.             return theVariance
  96.         else:
  97.             return None
  98.     #def variance
  99.  
  100.     #stdevp = sqrt(variance)
  101.     #stdevp = variance ** (1/2)
  102.     def standardDeviation(self):
  103.         if (self.howManyValues()>0):
  104.             #return self.variance()**(1/2)
  105.             variance = self.variance()
  106.             squareRoot = variance**(1/2)
  107.             return squareRoot
  108.         else:
  109.             return None
  110.         #else
  111.     #def standardDeviation
  112.  
  113.     """
  114.    example:
  115.    working on list [10, 20, 40, 40]
  116.    set([10, 20, 40, 40]) --> [10, 20, 40]
  117.    working over a collection without repetitions...
  118.    set
  119.    the return should be this dictionary:
  120.    {10:1, 20:1, 40:2}
  121.    """
  122.     def absoluteFrequencyOfElements(self):
  123.         retDict = {}
  124.         if (self.howManyValues()>0):
  125.             setWithoutRepitions = set(self.mList) #[10, 20, 40] #optimization => does not question repeated elements
  126.             #for v in self.mList: #[10, 20, 40, 40] #would count 40 twice
  127.             for v in setWithoutRepitions:
  128.                 iCount = self.count(v)
  129.                 retDict[v] = iCount
  130.  
  131.                 #retDict[v] = self.count(v)
  132.             #for
  133.             return retDict
  134.         else:
  135.             return None
  136.     #def absoluteFrequencyOfElements
  137.  
  138.     def count(self, pEl):
  139.         bShouldWork = self.howManyValues() and \
  140.                       pEl!=None
  141.         if (bShouldWork):
  142.             iCounter = 0
  143.             for v in self.mList:
  144.                 if (pEl==v):
  145.                     iCounter+=1
  146.                 #if
  147.             #for
  148.             return iCounter
  149.         else:
  150.             return 0
  151.     #def count
  152.  
  153.     def mostFrequentValues (self):
  154.         #dictionary of absolute frequencies
  155.         daf = self.absoluteFrequencyOfElements()
  156.         if (daf!=None):
  157.             theKeys = daf.keys()
  158.             theGreatest = None
  159.             for k in theKeys:
  160.                 v = daf[k]
  161.                 if (theGreatest is None):
  162.                     theGreatest = v
  163.                 else:
  164.                     if (v>theGreatest):
  165.                         theGreatest = v
  166.                     #if
  167.                 #else
  168.             #for
  169.  
  170.             #what do I have?????
  171.             #in var theGreatest we capture the most
  172.             #frequent value in the collection (self.mList)
  173.  
  174.             listMode = []
  175.             for k in theKeys:
  176.                 if (daf[k]==theGreatest):
  177.                     listMode.append(k)
  178.                 #if
  179.             #for
  180.             return listMode
  181.         else:
  182.             return None
  183.     #def mostFrequentValues
  184.  
  185.     #TPC: leastFrequentValues
  186.  
  187.     def mode(self):
  188.         return self.mostFrequentValues()
  189.     #def
  190. #class MyList
  191.  
  192. l1 = MyList()
  193. l2 = MyList([10, 20, 30])
  194. smallestInL1 = l1.min()
  195. greatestInL2 = l2.max()
  196.  
  197. print ("l1: ", l1)
  198. print ("l2: ", l2)
  199.  
  200. print ("smallest in l1: ", smallestInL1)
  201. print ("greatest in l2: ", greatestInL2)
  202.  
  203. print ("How many elements in l1: ",
  204.     l1.howManyValues()
  205. )
  206.  
  207. print ("How many elements in l2: ",
  208.     l2.howManyValues()
  209. )
  210.  
  211. theMeanL1 = l1.mean()
  212. theMeanL2 = l2.mean()
  213.  
  214. print ("The mean of l1: ", theMeanL1)
  215. print ("The mean of l2: ", theMeanL2)
  216.  
  217. l3 = MyList([20, 20, 40, 40, 50, 50, 50])
  218. v3 = l3.variance()
  219. dp3 = l3.standardDeviation()
  220. print("The variance of {} is {}".format(l3, v3))
  221. print("The standard deviation of {} is {}".\
  222.       format(l3, dp3))
  223.  
  224. daf = l3.absoluteFrequencyOfElements()
  225. print (daf)
  226.  
  227. theModeList = l3.mode()
  228. print ("The mode (as a list) for {} is {}".\
  229.        format(l3, theModeList))
  230.  
  231. l2 = MyList()
  232. l44 = MyList([44, 44, 44])
  233. l2.mode()
  234. l44.mode()
  235.  
  236. #the left-operand is NOT an instance of the class
  237. #but the class's name
  238. print(MyList.BATATA) #static
RAW Paste Data