Advertisement
ThegeekKnight16

Calculador de média

Apr 24th, 2023
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.81 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as pt
  3. class Codigo:
  4.     def __init__(self, codigo = "", notasC = []):
  5.         self.codigo = codigo
  6.         self.notasC = notasC
  7.  
  8.     def getAverage(self):
  9.         s = 0.0
  10.         for x in self.notasC:
  11.             s += x;
  12.         return (s / len(self.notasC))
  13.  
  14.     def printGrades(self):
  15.         print("-{}: {} --> Average: {:.2f}".format(self.codigo, self.notasC, self.getAverage()))
  16.  
  17.     def getGroup(self):
  18.         f = self.codigo[0];
  19.         s = self.codigo[1];
  20.         t = self.codigo[2];
  21.         frth = 'z';
  22.         if len(self.codigo) >= 4:
  23.             frth = self.codigo[3];
  24.  
  25.         if f == 'B':
  26.             return "Biologia";
  27.         if f == 'F':
  28.             return "Fisica";
  29.         if f == 'Q':
  30.             return "Quimica";
  31.         if f == 'M':
  32.             return "Matematica";
  33.         if f == 'C':
  34.             return "Compl. de Matematica";
  35.         if f == 'G':
  36.             return "Geografia";
  37.         if f == 'I':
  38.             return "SAT/TOEFL";
  39.         if f == 'E':
  40.             return "Artes";
  41.         if f == 'L':
  42.             return "Laboratorio";
  43.         if f == 'H':
  44.             if t == '3':
  45.                 return "Sociologia";
  46.             if t == '4':
  47.                 return "Atualidades";
  48.             return "Historia";
  49.         if f == 'P':
  50.             if s == 'G':
  51.                 return "Prova Geral";
  52.             if s == 'R':
  53.                 return "Prova de Redacao";
  54.             if t == '5':
  55.                 return "Filosofia";
  56.             return "Portugues";
  57.         if f == 'T':
  58.             if frth == 'z':
  59.                 return "Temas Diversos";
  60.             if t == '4':
  61.                 if frth == '0':
  62.                     return "AP Literature";
  63.                 if frth == '1':
  64.                     return "AP Human Geography";
  65.                 if frth == '2':
  66.                     return "AP Psychology";
  67.                 if frth == '3':
  68.                     return "AP CS Principles";
  69.                 if frth == '4':
  70.                     return "AP French";
  71.             if frth == '0':
  72.                 return "AP Language";
  73.             if frth == '1':
  74.                 return "AP Calculus";
  75.             if frth == '2':
  76.                 return "AP Chemistry";
  77.             if frth == '3':
  78.                 return "AP Biology";
  79.             if frth == '4':
  80.                 return "AP Statistics";
  81.             if frth == '5':
  82.                 return "AP Physics";
  83.             if frth == '6':
  84.                 return "AP World History";
  85.             if frth == '7':
  86.                 return "AP CS A";
  87.             if frth == '8':
  88.                 return "AP Latin";
  89.             if frth == '9':
  90.                 return "AP Microeconomics";
  91.         if f == 'V':
  92.             return "Verificacao Final";
  93.         return self.codigo
  94.  
  95.     def getSum(self):
  96.         s = 0.0
  97.         for x in self.notasC:
  98.             s += x
  99.         return s
  100.  
  101.     def getAmount(self):
  102.         return len(self.notasC)
  103.  
  104.     def insertGrade(self, s):
  105.         self.notasC.append(s)
  106.  
  107.     def __lt__ (self, other):
  108.         return (self.getAverage() < other.getAverage())
  109.  
  110. class Materia:
  111.     def __init__(self, nome = "", sum = 0.0, qtd = 0):
  112.         self.nome = nome
  113.         self.sum = sum
  114.         self.qtd = qtd
  115.  
  116.     def insertGrades(self, a, b):
  117.         self.sum += a
  118.         self.qtd += b
  119.  
  120.     def getSum(self):
  121.         return self.sum
  122.  
  123.     def getAmount(self):
  124.         return self.qtd
  125.  
  126.     def getTotalAverage(self):
  127.         return (self.sum / self.qtd)
  128.  
  129.     def printAverage(self):
  130.         print("-{}: -> {:.2f}".format(self.nome, self.getTotalAverage()))
  131.  
  132.     def getGp(self):
  133.         return self.nome
  134.  
  135.     def setGroup(self, s):
  136.         self.nome = s
  137.  
  138.     def __lt__ (self, other):
  139.         return (self.getTotalAverage() < other.getTotalAverage())
  140.  
  141. f = open("input.txt", "r")
  142.  
  143. lines = f.readlines()
  144. notas = []
  145. codigos = []
  146. areas = []
  147.  
  148. for materia in lines:
  149.     aux = materia.split()
  150.     # print(aux)
  151.  
  152.     curname = ""
  153.     curgrade = []
  154.  
  155.     for s in aux:
  156.         # print(s)
  157.         if s[0].isalpha() and s[-1].isdigit():
  158.             curname = s
  159.         elif s[0].isdigit() and s[-1].isdigit() and len(curname) > 0 and not curname.startswith("AR"):
  160.             curgrade.append((float(s)))
  161.             notas.append(float(s))
  162.  
  163.     if 0 < len(curname) <= 4 and not curname.startswith('AR'):
  164.         codigos.append(Codigo(curname, curgrade))
  165.  
  166. for c in codigos:
  167.     achou = False
  168.     for m in areas:
  169.         if c.getGroup() == m.getGp():
  170.             m.insertGrades(c.getSum(), c.getAmount())
  171.             achou = True
  172.             break
  173.  
  174.     if achou == True:
  175.         continue
  176.  
  177.     aux = Materia()
  178.     aux.setGroup(c.getGroup())
  179.     aux.insertGrades(c.getSum(), c.getAmount())
  180.     areas.append(aux)
  181.  
  182. GPA = 0.0
  183. for x in notas:
  184.     GPA += x
  185.  
  186. n = len(notas)
  187.  
  188. print("GRADE REPORT: \n")
  189. print("Your GPA is: {:.2f} ({} grades)\n".format(GPA / n, n))
  190.  
  191. codigos.sort()
  192. notas.sort()
  193. areas.sort()
  194.  
  195. print("STATISTICS:")
  196.  
  197.  
  198. print("-MIN: {:.2f}".format(notas[0]))
  199. print("-Q1: {:.2f}".format(notas[int(n / 4)]))
  200. print("-Q2 (MEDIAN): {:.2f}".format(notas[int(n / 2)]))
  201. print("-Q3: {:.2f}".format(notas[n - 1 - int(n / 4)]))
  202. print("-MAX: {:.2f}".format(notas[n-1]))
  203. print("-RANGE: {:.2f}".format(notas[n - 1] - notas[0]))
  204. print("-IQR: {:.2f}".format(notas[n - 1 - int(n / 4)] - notas[int(n / 4)]))
  205. print("\n")
  206.  
  207. print("PER SUBJECT (SORTED BY AVERAGE GRADE): ")
  208.  
  209. for c in codigos:
  210.     c.printGrades()
  211.  
  212. print("\nAVERAGE PER AREA (SORTED BY AVERAGE GRADE):")
  213.  
  214. for m in areas:
  215.     m.printAverage()
  216.  
  217. print("\n\nDELTAS TABLE:\n")
  218.  
  219. for nextGrade in np.arange(0.0, 10.5, 0.5):
  220.     newGPA = (GPA + nextGrade)/(n+1)
  221.     print("{:.2f} --> {:.2f} and delta {:.2f}".format(nextGrade, newGPA ,newGPA - (GPA / n)))
  222.  
  223. pt.hist(notas)
  224. pt.show()
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement