Advertisement
Guest User

esercizio.py

a guest
Jul 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. import traceback
  2. import math
  3.  
  4. def leggi_voti(filename):
  5.   """
  6.  return a tuple (int, int, bool) from file
  7.  
  8.  format must be (parsable_int) (parsable_int or string) (Optional boolean)
  9.  """
  10.   voti = []
  11.   with open(filename, 'r') as f:
  12.       for line in f:
  13.           tokens = line.strip().split()
  14.          
  15.           matricola = int(tokens[0])
  16.           voto      = int(tokens[1]) if tokens[1].isdigit() and int(tokens[1]) >= 18 else -1 # means not sufficient !USE SAME TYPE
  17.           lode = True if len(tokens) > 2 and bool(tokens[2]) == True else False
  18.  
  19.           voti.append((matricola, voto, lode))
  20.  
  21.   return voti
  22.  
  23.  
  24.  
  25. def only_sufficienti (lista_voti):
  26.   """
  27.  restituisce una lista di persone sufficienti:
  28.  
  29.  es:
  30.    >>> only_sufficienti([(1, 18, False), (2, 30, True), (3, 15, False)])
  31.    [(1, 18, False), (2, 30, True)]
  32.  """
  33.   return [x for x in lista_voti if x[1] > 0]
  34.  
  35.  
  36.  
  37. def media_sufficienti(lista_voti):
  38.     '''Deve restituire il voto medio considerando solo
  39.   gli studenti che hanno ottenuto un punteggio sufficiente
  40.   '''
  41.     # L = []
  42.     # n = 0
  43.     # votox = True
  44.     # for i in range(len(lista_voti)): #NO!!!!!
  45.     #     if lista_voti[i][1] != 'ins':
  46.     #         votox == False
  47.     #         if votox == True:
  48.     #             L.append(lista_voti[i][1])
  49.     #             n += 1
  50.     # media =(sum(L))/n
  51.     # return media
  52.  
  53.     sufficienti = only_sufficienti(lista_voti) # filter only interested items
  54.     return sum([x[1] for x in sufficienti]) / len(sufficienti) # sum / count
  55.  
  56. def media_e_std_sufficienti(lista_voti):
  57.     '''Deve restituire il voto medio e la deviazione standard dalla media
  58.   considerando solo gli studenti che hanno ottenuto un punteggio
  59.   sufficienti pastepin
  60.   '''
  61.     # L = []
  62.     # n = 0
  63.     # votox = True
  64.     # for i in range(len(lista_voti)): # NO!!!!!
  65.     #     if type(lista_voti[i][1]) == int: # use isinstance, ALSO USE THE SAME TYPE
  66.     #         votox == False # ??? (facepalm)
  67.     #         if votox == True:
  68.     #             L.append(lista_voti[i][1])
  69.     #             n += 1
  70.     # media =(sum(L))/n
  71.     # from math import sqrt
  72.     # std = 0
  73.     # for i in range(n):
  74.     #     std += (L[i]-media)**2
  75.     # std = sqrt(std/n)
  76.     # return std,media
  77.  
  78.     sufficienti = only_sufficienti(lista_voti)
  79.     media = media_sufficienti(sufficienti)
  80.  
  81.     deviazione = sum([(x[1] - media) ** 2 for x in sufficienti]) / len(sufficienti)
  82.  
  83.     return media, deviazione
  84.  
  85.  
  86. if __name__ == '__main__':
  87.   filename = 'voti_studenti.txt'
  88.  
  89.   try:
  90.     voti = leggi_voti(filename)
  91.  
  92.     sufficienti = media_sufficienti(voti)
  93.     print('sufficienti:', sufficienti)
  94.  
  95.     std_sufficienti = media_e_std_sufficienti(voti)
  96.     print('std sufficienti:', std_sufficienti)
  97.    
  98.  
  99.   except FileNotFoundError as e:
  100.     print(f'file {filename} does not exists')
  101.    
  102.   except Exception as e:
  103.     traceback.print_exc()
  104.     print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement