2015-01-25 22:54:28 +01:00
# BEAUSSART Jean-loup
2015-01-25 22:44:15 +01:00
# PREUD'HOMME Geoffrey
# Donné le 20/01/2015
2015-01-25 22:54:28 +01:00
# TP1 Gestion d'une promotion d'étudians - Structures itérables
2015-01-25 22:51:10 +01:00
2015-01-25 22:46:34 +01:00
# http://www.fil.univ-lille1.fr/~L1S2API/CoursTP/tp1_structures_iterables.html
from etudiants import *
2015-01-25 23:01:08 +01:00
def question ( numero ) :
print ( ' \n *** ' , ' Question ' , numero , " *** " )
2015-01-25 22:46:34 +01:00
2015-01-25 23:01:08 +01:00
question ( 1 )
2015-01-25 22:46:34 +01:00
2015-01-25 23:28:57 +01:00
test1 = type ( l_etudiants ) == list
2015-01-25 22:46:34 +01:00
for i in l_etudiants :
2015-01-25 23:28:57 +01:00
if not ( type ( i ) == tuple and len ( i ) == 5 and type ( i [ 4 ] ) == int ) :
2015-01-25 23:18:03 +01:00
test1 = False
for j in range ( 4 ) :
2015-01-25 23:28:57 +01:00
if not type ( i [ j ] ) == str :
2015-01-25 23:18:03 +01:00
test1 = False
2015-01-25 22:46:34 +01:00
2015-01-25 23:18:03 +01:00
print ( ' Le test a retourné ' , test1 )
2015-01-25 22:46:34 +01:00
2015-01-25 23:01:08 +01:00
question ( 2 )
2015-01-25 22:46:34 +01:00
2015-01-25 23:23:12 +01:00
print ( " Il y a " , len ( l_etudiants ) , " étudiants dans la liste " )
2015-01-25 22:46:34 +01:00
2015-01-25 23:01:08 +01:00
question ( 3 )
2015-01-25 22:46:34 +01:00
2015-01-25 23:23:12 +01:00
monId = 11501230 # Jean-Loup
# monId = 11500683 # Geoffrey
2015-01-25 22:46:34 +01:00
2015-01-25 23:23:12 +01:00
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 ] )
2015-01-25 22:46:34 +01:00
2015-01-25 22:44:15 +01:00
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 )
2015-01-25 23:18:03 +01:00
2015-01-25 22:44:15 +01:00
question ( 5 )
def nbre_prenoms ( prenom ) :
"""
str → int
Renvoie le nombre d ’ étudiants dont le prénom est passé en paramètre
2015-01-25 23:28:57 +01:00
CU : prenom est un str
2015-01-25 22:44:15 +01:00
"""
2015-01-25 23:28:57 +01:00
assert ( type ( prenom ) == str ) , " prenom n ' est pas du type str "
2015-01-25 23:18:03 +01:00
return [ i [ 2 ] for i in l_etudiants ] . count ( prenom . upper ( ) )
2015-01-25 22:46:34 +01:00
2015-01-25 23:18:03 +01:00
print ( " Il y a " , nbre_prenoms ( " Alexandre " ) , " étudiants qui s ' appellent Alexandre " )
2015-01-25 23:28:57 +01:00
print ( " Il y a " , nbre_prenoms ( " Camille " ) , " étudiant(e)s qui s ' appellent Camille " )
2015-01-25 22:46:34 +01:00
2015-01-25 22:44:15 +01:00
question ( 6 )
2015-01-25 22:46:34 +01:00
2015-01-25 23:18:03 +01:00
ensemblePrenoms = set ( [ i [ 2 ] for i in l_etudiants ] )
2015-01-25 22:44:15 +01:00
print ( ' Il y a ' , len ( ensemblePrenoms ) , ' prénoms différents parmi tous les étudiants ' )
question ( 7 )
2015-01-25 23:28:57 +01:00
# 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"
2015-01-25 22:44:15 +01:00
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 )
2015-01-25 23:28:57 +01:00
# Pour tester le cas où plusieurs prénoms arriveraient à la même quantité, décommentez la ligne suivante
# prenomsPlusFrequent.add('MAURICE')
2015-01-25 22:44:15 +01:00
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 ) )
2015-01-25 23:18:03 +01:00
2015-01-25 22:44:15 +01:00
question ( 8 )
2015-01-25 23:18:03 +01:00
idUniques = set ( i [ 0 ] for i in l_etudiants )
2015-01-25 22:44:15 +01:00
2015-01-25 23:18:03 +01:00
test8 = len ( idUniques ) != len ( l_etudiants )
2015-01-25 22:44:15 +01:00
2015-01-25 23:18:03 +01:00
print ( ' Le test a retourné ' , test8 )
2015-01-25 22:44:15 +01:00
question ( 9 )
2015-01-25 23:18:03 +01:00
# TODO On peut tout faire en une ligne
2015-01-25 22:44:15 +01:00
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 ) :
"""
2015-01-25 23:18:03 +01:00
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
2015-01-25 22:44:15 +01:00
"""
2015-01-25 23:18:03 +01:00
assert ( formation in parcours ) , " La formation donnée n ' est pas valide "
2015-01-25 23:28:57 +01:00
2015-01-25 22:44:15 +01:00
return list ( i [ 0 : 2 ] + ( i [ 4 ] , ) for i in l_etudiants if i [ 3 ] == formation )