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 Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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) is list
for i in l_etudiants:
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
print('Le test a retourné', test1)
question(2)
print("Il y a ", len(l_etudiants), " étudiants dans la liste")
question(3)
# TODO
print(l_etudiants[11501230%len(l_etudiants)])
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)
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"), "étudiantes 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)
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)
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)