# Jean-loup Beaussart # TP1: Gestion d'une promo d'étudiants - Structures itérables # coding=utf-8 # PREUD'HOMME Geoffrey # Donné le 20/01/2015 # TP1 Gestion d'une promotion d'étudians # http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp1_structures_iterables.html from etudiants import * # Question 1 print("\n *** Question 1 ***") assert(type(l_etudiants) == type([])) for i in l_etudiants: assert(type(i) == type(tuple())) assert(len(i) == 5) for j in i: assert(type(i[0]) == type(i[1]) == type(i[2]) == type(i[3]) == type("")) assert(type(i[4]) == type(1)) print("La liste l_etudiant est correcte") # Question 2 print("\n *** Question 2 ***") print("Il y a ", len(l_etudiants), " étudiants dans la liste") # Question 3 print("\n *** Question 3 ***") print(l_etudiants[11501230%len(l_etudiants)]) # Question 4 print("\n *** Question 4 ***") formations = ["LICAM", "MASS", "PEIP", "SESI"] erreur = False for i in l_etudiants: if not i[3] in formations: print("L'étudiant ", i[1], " ", i[2], " n'est pas dans une des quatre formations.") erreur = True if not erreur: print("Tous les étudiants sont dans une formation.") # Question 5 print("\n *** Question 5 ***") def nbre_prenoms(prenom): """ str -> int, renvoie le nombre d'étudiants qui ont prenom comme prénom. CU: prenom est un str """ assert(type(prenom) == type("")) prenomARechercher = prenom.upper() nb=0 for i in l_etudiants: if i[2] == prenomARechercher: nb += 1 return nb print("Il y a ", nbre_prenoms("Alexandre"), " étudiants qui s'appellent Alexandre") print("Il y a ", nbre_prenoms("Camille"), " étudiantes qui s'appellent Camille") # Question 6 print("\n *** Question 6 ***") prenomsUniques = set() for i in l_etudiants: prenomsUniques.add(i[2]) print("Il y a ", len(prenomsUniques), " prénoms différents") # Question 7 print("\n *** Question 7 ***") lPrenoms=[i[2] for i in l_etudiants] freq = ["", 0] for i in prenomsUniques: tmp = lPrenoms.count(i) if tmp > freq[1]: freq = i, tmp print("Le prénom le plus courant est ", freq[0], " avec ", freq[1], " personnes portant ce prénom.") # Question 8 print("\n *** Question 8 ***") idUniques = set() for i in l_etudiants: idUniques.add(i[0]) if len(idUniques) != len(l_etudiants): print("Erreur, il existe au moins un id qui n'est pas unique") else: print("Tous les id sont uniques") # Question 9 print("\n *** Question 9 ***") nbLICAM = nbMASS = nbPEIP = nbSESI = 0 for i in l_etudiants: if i[3] == "LICAM": nbLICAM += 1 elif i[3] == "MASS": nbMASS += 1 elif i[3] == "PEIP": nbPEIP += 1 else: nbSESI += 1 print("Il y a ", nbLICAM, " étudiants en LICAM ", nbMASS, " en MASS ", nbPEIP, " en PEIP et ", nbSESI, " en SESI.") # Question 10 print("\n *** Question 10 ***") def liste_formation(form): """ str -> liste de quadruplets, renvoie la liste des étudiants dans la formation passée en paramètre CU: form est un str qui vaut: SESI, LICAM, MASS ou PEIP """ assert(type(form) == type("")) assert(form == "SESI" or form == "PEIP" or form == "LICAM" or form == "MASS") ret = list() for i in l_etudiants: if i[3] == form: ret.append((i[0], i[1], i[2], i[4])) return ret print("Liste des PEIP:\n", liste_formation("PEIP")) # Geoffrey def question(numero): print('\n***', 'Question', numero, "***") question(1) test = type(l_etudiants) is list for i in l_etudiants: if not (type(i) is tuple and len(i) == 5 and type(i[4]) is int): test = False for j in range(4): if not type(i[j]) is str: test = False print('Le test a retourné', test) question(2) print('l_etudiants', 'contient', len(l_etudiants), 'étudiants') question(3) # TODO print('L\'indice de ma fiche étudiant est forcément inférieur au nombre de fiche, diviser ce premier nombre par ce deuxième revient à garder le premier') question(4) test4 = True for i in l_etudiants: if not i[3] in ('LICAM', 'MASS', 'PEIP', 'SESI'): test4 = False print('Le test a retourné', test4) question(5) def nbre_prenoms(prenom): """ str → int Renvoie le nombre d’étudiants dont le prénom est passé en paramètre """ ## n = 0 ## for i in l_etudiants: ## if i[2] == prenom: ## n += 1 ## return n return [i[2] for i in l_etudiants].count(prenom) nbreAlexandre = nbre_prenoms('Alexandre') terminaisonAlexandre = 's' if nbreAlexandre > 1 else '' print('Il y a', nbreAlexandre, 'étudiant' + terminaisonAlexandre, 'nommé' + terminaisonAlexandre, 'Alexandre') nbreCamille = nbre_prenoms('Camille') terminaisonCamille = 'e' + 's' if nbreCamille > 1 else '' print('Il y a', nbreCamille, 'étudiant' + terminaisonCamille, 'nommé' + terminaisonCamille, 'Camille') question(6) listePrenoms = [i[2] for i in l_etudiants] ensemblePrenoms = set(listePrenoms) print('Il y a', len(ensemblePrenoms), 'prénoms différents parmi tous les étudiants') question(7) nbresPrenoms = dict((i, nbre_prenoms(i)) for i in ensemblePrenoms) prenomsPlusFrequent = set() nbrePrenomsPlusFrequents = 0 for i in nbresPrenoms: if nbresPrenoms[i] > nbrePrenomsPlusFrequents: prenomsPlusFrequent = {i} nbrePrenomsPlusFrequents = nbresPrenoms[i] elif nbresPrenoms[i] == nbrePrenomsPlusFrequents: prenomsPlusFrequent.add(i) terminaison = 's' if len(prenomsPlusFrequent) > 1 else '' print('Le'+terminaison+' prénom le'+terminaison+' plus fréquent'+terminaison, 'sont' if len(prenomsPlusFrequent) > 1 else 'est', ', '.join(prenomsPlusFrequent)) question(8) ids = set() test8 = True for i in l_etudiants: if i[0] in ids: test = False else: ids.add(i[0]) question(9) parcours = dict() for i in l_etudiants: if not i[3] in parcours: parcours[i[3]] = 0 parcours[i[3]] += 1 print('Il y a', ', '.join(list(str(parcours[i])+' étudiants en '+i for i in parcours))+'.') question(10) def liste_formation(formation): """ ... """ return list(i[0:2]+(i[4],) for i in l_etudiants if i[3] == formation) print(liste_formation('PEIP'))