123 lines
3.4 KiB
Python
123 lines
3.4 KiB
Python
# BEAUSSART Jean-loup
|
||
# PREUD'HOMME Geoffrey
|
||
# Donné le 20/01/2015
|
||
# TP1 Gestion d'une promotion d'étudians - Structures itérables
|
||
|
||
# http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp1_structures_iterables.html
|
||
|
||
from etudiants import *
|
||
|
||
def question(numero):
|
||
print('\n***', 'Question', numero, "***")
|
||
|
||
question(1)
|
||
|
||
test1 = type(l_etudiants) == list
|
||
for i in l_etudiants:
|
||
if not (type(i) == tuple and len(i) == 5 and type(i[4]) == int):
|
||
test1 = False
|
||
for j in range(4):
|
||
if not type(i[j]) == str:
|
||
test1 = False
|
||
|
||
|
||
print('Le test a retourné', test1)
|
||
|
||
|
||
question(2)
|
||
|
||
print("Il y a", len(l_etudiants), "étudiants dans la liste")
|
||
|
||
|
||
question(3)
|
||
|
||
monId = 11501230 # Jean-Loup
|
||
# monId = 11500683 # Geoffrey
|
||
|
||
fiche = l_etudiants[monId%len(l_etudiants)]
|
||
print("Cette fiche est celle de", fiche[2], fiche[1], "qui est en", fiche[3], fiche[4], "et son id est le", fiche[0])
|
||
|
||
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
|
||
CU : prenom est un str
|
||
"""
|
||
assert(type(prenom) == str), "prenom n'est pas du type str"
|
||
|
||
return [i[2] for i in l_etudiants].count(prenom.upper())
|
||
|
||
print("Il y a", nbre_prenoms("Alexandre"), "étudiants qui s'appellent Alexandre")
|
||
print("Il y a", nbre_prenoms("Camille"), "étudiant(e)s qui s'appellent Camille")
|
||
|
||
question(6)
|
||
|
||
ensemblePrenoms = set([i[2] for i in l_etudiants])
|
||
print('Il y a', len(ensemblePrenoms), 'prénoms différents parmi tous les étudiants')
|
||
|
||
question(7)
|
||
|
||
# La question demande "quel est le prénom le plus fréquent", nous nous sommes permis ici de répondre à la question "quels sont les prénoms les plus fréquents"
|
||
|
||
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)
|
||
|
||
# Pour tester le cas où plusieurs prénoms arriveraient à la même quantité, décommentez la ligne suivante
|
||
# prenomsPlusFrequent.add('MAURICE')
|
||
|
||
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)
|
||
|
||
idUniques = set(i[0] for i in l_etudiants)
|
||
|
||
test8 = len(idUniques) != len(l_etudiants)
|
||
|
||
print('Le test a retourné', test8)
|
||
|
||
question(9)
|
||
|
||
# TODO On peut tout faire en une ligne
|
||
|
||
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):
|
||
"""
|
||
str → list[tuple(str, str, str, int)]
|
||
Renvoie la liste des quadruplets (id, nom, prenom, gpe) correspondants à tous les étudiants appartenant à la formation donnée
|
||
CU : formation est une formation valide
|
||
"""
|
||
assert(formation in parcours), "La formation donnée n'est pas valide"
|
||
|
||
return list(i[0:2]+(i[4],) for i in l_etudiants if i[3] == formation)
|