This repository has been archived on 2019-08-09. You can view files and clone it, but cannot push or open issues or pull requests.
s1-tp/S2/TP1/tp1.py

118 lines
2.9 KiB
Python
Raw Normal View History

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: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:18:03 +01:00
test1 = type(l_etudiants) is list
2015-01-25 22:46:34 +01:00
for i in l_etudiants:
2015-01-25 23:18:03 +01:00
if not (type(i) is tuple and len(i) == 5 and type(i[4]) is int):
test1 = False
for j in range(4):
if not type(i[j]) is str:
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
print("Il y a ", len(l_etudiants), " étudiants dans la liste")
2015-01-25 23:01:08 +01:00
question(3)
2015-01-25 22:46:34 +01:00
2015-01-25 23:18:03 +01:00
# TODO
2015-01-25 22:46:34 +01:00
print(l_etudiants[11501230%len(l_etudiants)])
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:18:03 +01:00
CU: prenom est un str
2015-01-25 22:44:15 +01:00
"""
2015-01-25 23:18:03 +01:00
assert(type(prenom) == str)
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")
print("Il y a", nbre_prenoms("Camille"), "étudiantes 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:46:34 +01:00
2015-01-25 22:44:15 +01:00
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))
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 22:44:15 +01:00
return list(i[0:2]+(i[4],) for i in l_etudiants if i[3] == formation)