Tp1
This commit is contained in:
parent
e3d50d5e57
commit
cb3e861492
170
S2/TP1/tp1.py
Normal file
170
S2/TP1/tp1.py
Normal file
|
@ -0,0 +1,170 @@
|
|||
# Jean-loup Beaussart
|
||||
# 20/01/2015
|
||||
# TP1: Gestion d'une promo d'étudiants - Structures itérables
|
||||
# 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"))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in a new issue