Vos programmes devront reproduire exactement les exemples fournis
Les étudiants du master de Génie Algorithmique Généraliste Avancé, dont vous êtes responsable pédagogique, viennent de passer leur épreuve de COBOL, et les correcteurs vous ont envoyé le fichier notes.txt. L'épreuve comportait 17 questions, de difficultés variées, et on demandait aux correcteurs de noter chaque réponse par un entier de 0 à 4. La note finale sera calculée en appliquant une liste de coefficients, initialement fixée à
coeffs = [2, 3, 3, 4, 4, 2, 1, 5, 5, 3, 1, 3, 2, 5, 5, 4, 3]
ce qui donne donc une note maximale de 220 points, à ramener sur 20 en arrondissant au demi-point le plus proche.
1) Écrivez une fonction parse(ligne,coeffs)
qui convertit une ligne du fichier en un dictionnaire sont les clés sont nom
, prenom
, note
. On considérera que les prénoms forment le premier mot de chaque ligne, alors que les noms de famille peuvent être composés de plusieurs mots, et on vérifiera au moyen d'une clause assert
qu'il y a bien une note pour chacune des 17 questions.
Par exemple, si ligne = 'Alexandre Garnier de la Coste\t\t3 3 3 4 2 0 1 3 2 1 2 2 0 1 0 0 0'
, le résultat doit être
{'prenom': 'Alexandre', 'nom': 'Garnier de la Coste', 'note': 8.0}
coeffs = [2, 3, 3, 4, 4, 2, 1, 5, 5, 3, 1, 3, 2, 5, 5, 4, 3]
s = open('notes.txt','r').read()
print(s[:200])
Lucie Fouquet 2 4 0 4 3 2 4 1 3 1 0 2 2 4 1 0 2 Augustin de la Gonzalez 3 0 4 1 2 4 2 3 1 3 2 3 2 3 4 1 0 Margaret Boulanger du Munoz 3 4 1 1 2 2 2 1 4 0 0 1 0 0 2 3 0 Christine Pires 4 4 2 4 2 4
def parse(line,coeffs):
res = {}
aa = line.rstrip().split()
res['prenom'] = aa[0]; aa = aa[1:]
nn = aa[-17:]; assert all(n.isdigit() for n in nn)
res['nom'] = ' '.join(aa[:-17])
res['note']= round(10*sum(int(n)*c for n,c in zip(nn,coeffs))/(sum(coeffs)))/2
return res
mm = s.split('\n')
mm[66]
'Alexandre Garnier de la Coste\t\t3 3 3 4 2 0 1 3 2 1 2 2 0 1 0 0 0'
parse(mm[66], coeffs)
{'prenom': 'Alexandre', 'nom': 'Garnier de la Coste', 'note': 8.0}
2) Formez la liste ee
des dictionnaires associés à chaque étudiant, et écrivez une fonction tri_nom
qui renvoie une liste de lignes NOM Prénom note
triée par ordre alphabétique des noms. Par exemple, for l in tri_nom(ee): print(l)
devra afficher
ALLAIN Patricia-Josette 9.0
ARNAUD Patricia 13.0
AUBERT Michelle 6.5
BARON DE LOPEZ Alain 6.5
...
VALLET Valentine 9.5
VALLET-HERVÉ Madeleine 10.0
WAGNER Laetitia 11.5
On prendra soin de convertir les noms en majuscules avant de trier, car 'Z'<'a'
.
ee = [parse(e,coeffs) for e in mm]
def tri_nom(ee):
return sorted([e['nom'].upper()+' '+e['prenom']+' '+ str(e['note']) for e in ee])
ll1 = tri_nom(ee)
for x in ll1: print(x)
ALLAIN Patricia-Josette 9.0 ARNAUD Patricia 13.0 AUBERT Michelle 6.5 BARON DE LOPEZ Alain 6.5 BARTHELEMY-THOMAS Susanne 9.5 BENOIT-MULLER Sébastien 10.5 BERNARD Claude 10.0 BIGOT David 8.0 BLANCHARD Benoît 9.5 BLOT DE LA CLÉMENT Richard 10.5 BODIN Édouard 10.5 BOUCHET-ÉTIENNE Georges 7.5 BOULANGER DU MUNOZ Margaret 8.0 BOULAY Bertrand 12.0 BOURDON Noël 8.0 BOURDON Thibault 9.0 BOUSQUET Margaret 9.0 BRETON-PASCAL Roger 8.5 BRUN-LÉVÊQUE Lucas 9.5 BRUNEL Antoinette 7.0 CARLIER Alice 10.5 CHARTIER Aimé 9.5 COHEN Alain 12.5 COLLIN Bertrand 10.5 COLLIN Jeannine-Anne 7.0 COURTOIS Franck 8.0 COUTURIER Thibault 7.0 DA COSTA Xavier 8.0 DE BERNIER Renée 10.0 DE LA COSTE Agnès 8.0 DE LA GONZALEZ Augustin 11.0 DE LA HEBERT André 12.0 DE LA LAROCHE Sébastien 8.5 DE LA MERCIER Amélie 8.5 DE MASSE Arnaude 8.5 DELAHAYE Antoine 11.5 DELORME DE BAILLY Lucy 8.5 DESCHAMPS DU HUMBERT Thibault 9.5 DIAZ Éléonore-Bernadette 10.5 DU JEAN Margaret 9.0 DU MARCHAND Adrien 12.5 DU ROSSI René 12.5 EVRARD-BENARD Philippe 12.0 FOUQUET Lucie 10.5 FOURNIER Stéphane 12.0 GARNIER DE LA COSTE Alexandre 8.0 GAUDIN-LELEU Véronique 7.5 GEORGES Clémence 11.0 GERMAIN Céline 11.5 GILBERT-PICHON William 10.5 GIRARD Margaud 11.0 GOMEZ LE BOUCHER Mathilde 8.5 GONCALVES Louis 10.0 GUILLET DE DUHAMEL Isabelle 9.5 HOAREAU Célina 10.5 HUMBERT Antoinette 11.0 JACQUES Thibaut 11.5 KLEIN Aimé-Sébastien 10.5 LABBÉ Bertrand 9.5 LAMY Hortense 8.5 LE VALETTE Adrienne 10.0 LEFEBVRE Jules-Louis 9.5 LEFÈVRE Maryse 9.5 LEGRAND Louis 8.0 LESAGE Antoine-Thibaut 12.0 LOPES-GUYON Amélie 8.5 LOPEZ Nathalie 6.5 MACE Bernard-Xavier 10.0 MAILLARD-TOUSSAINT Patricia 11.5 MARCHAND Vincent 7.5 MARIN Adrienne 9.5 MARTY-THOMAS Yves 9.5 MARY-GÉRARD Édouard 7.0 MAURY Alix 8.0 MOREAU Denis 8.0 MOREL Eugène 12.0 MORVAN Benoît 11.0 NEVEU Marie 11.0 NICOLAS DE PRÉVOST Raymond 12.0 NORMAND Gabriel 7.5 OLLIVIER Olivier 6.5 PAUL Susan 13.0 PAYET Océane 8.5 PERRET Maurice 9.5 PERRIN Alix 9.0 PHILIPPE DE VAILLANT Lucy 10.5 PIRES Christine 10.5 POULAIN Denis-Émile 7.0 RIVIÈRE Hélène 10.0 ROBERT Alexandrie 9.5 ROBIN Alfred 9.0 ROUSSET Inès 9.5 ROUSSET Rémy 10.5 ROY Adèle-Jacqueline 8.5 SALMON Dorothée-Patricia 14.5 TANGUY-ROYER Honoré 10.5 TORRES Zacharie 7.5 VALLET Valentine 9.5 VALLET-HERVÉ Madeleine 10.0 WAGNER Laetitia 11.5
3) On veut maintenant afficher les statistiques de l'examen. Écrivez une fonction stats(ee)
qui affiche la moyenne, la médiane, le nombre de reçus, le nombre de collés, et le nombre total d'étudiants :
>>> stats(ee)
Moyenne = 9.625 Mediane = 9.5
45 reçus, 55 collés sur 100 étudiants.
def stats(ee):
nn = sorted([e['note'] for e in ee])
moy = sum(nn)/len(ee)
med = nn[len(nn)//2]
recus = len([e for e in ee if e['note']>=10])
colles = len([e for e in ee if e['note']<10])
total = len(ee)
print('Moyenne = ',moy, ' Mediane = ',med)
print(recus, ' reçus, ', colles, ' collés ', 'sur ', total, ' étudiants.')
stats(ee)
Moyenne = 9.625 Mediane = 9.5 45 reçus, 55 collés sur 100 étudiants.
4) On veut finalement ajouter une clé rang
dans les dictionnaires représentant les étudiants. Écrivez une fonction calcule_rangs(ee)
qui effectue cette opération. Deux étudiants ayant la même note devront avoir le même rang. Le classement devra être
1 SALMON Dorothée-Patricia 14.5
2 ARNAUD Patricia 13.0
2 PAUL Susan 13.0
4 COHEN Alain 12.5
4 DU MARCHAND Adrien 12.5
4 DU ROSSI René 12.5
7 BOULAY Bertrand 12.0
...
92 POULAIN Denis-Émile 7.0
97 AUBERT Michelle 6.5
97 BARON DE LOPEZ Alain 6.5
97 LOPEZ Nathalie 6.5
97 OLLIVIER Olivier 6.5
def calcule_rangs(ee):
rr = sorted(ee, key=lambda x:x['note'],reverse=True)
i = 1; rr[0].update({'rang':1})
for j in range(1,len(rr)):
e = rr[j]
if e['note']==rr[j-1]['note']: e.update({'rang':rr[j-1]['rang']})
else: e.update({'rang':j+1}) # e est modifié en place, pas besoin de return ...
calcule_rangs(ee)
ee
[{'prenom': 'Lucie', 'nom': 'Fouquet', 'note': 10.5, 'rang': 25}, {'prenom': 'Augustin', 'nom': 'de la Gonzalez', 'note': 11.0, 'rang': 19}, {'prenom': 'Margaret', 'nom': 'Boulanger du Munoz', 'note': 8.0, 'rang': 77}, {'prenom': 'Christine', 'nom': 'Pires', 'note': 10.5, 'rang': 25}, {'prenom': 'Alfred', 'nom': 'Robin', 'note': 9.0, 'rang': 61}, {'prenom': 'Alix', 'nom': 'Perrin', 'note': 9.0, 'rang': 61}, {'prenom': 'Noël', 'nom': 'Bourdon', 'note': 8.0, 'rang': 77}, {'prenom': 'Vincent', 'nom': 'Marchand', 'note': 7.5, 'rang': 87}, {'prenom': 'Denis-Émile', 'nom': 'Poulain', 'note': 7.0, 'rang': 92}, {'prenom': 'Antoine', 'nom': 'Delahaye', 'note': 11.5, 'rang': 14}, {'prenom': 'Alain', 'nom': 'Baron de Lopez', 'note': 6.5, 'rang': 97}, {'prenom': 'Patricia-Josette', 'nom': 'Allain', 'note': 9.0, 'rang': 61}, {'prenom': 'Clémence', 'nom': 'Georges', 'note': 11.0, 'rang': 19}, {'prenom': 'Margaud', 'nom': 'Girard', 'note': 11.0, 'rang': 19}, {'prenom': 'Lucy', 'nom': 'Philippe de Vaillant', 'note': 10.5, 'rang': 25}, {'prenom': 'David', 'nom': 'Bigot', 'note': 8.0, 'rang': 77}, {'prenom': 'Aimé-Sébastien', 'nom': 'Klein', 'note': 10.5, 'rang': 25}, {'prenom': 'Thibault', 'nom': 'Deschamps du Humbert', 'note': 9.5, 'rang': 46}, {'prenom': 'Thibault', 'nom': 'Couturier', 'note': 7.0, 'rang': 92}, {'prenom': 'Édouard', 'nom': 'Mary-Gérard', 'note': 7.0, 'rang': 92}, {'prenom': 'Benoît', 'nom': 'Morvan', 'note': 11.0, 'rang': 19}, {'prenom': 'Gabriel', 'nom': 'Normand', 'note': 7.5, 'rang': 87}, {'prenom': 'Aimé', 'nom': 'Chartier', 'note': 9.5, 'rang': 46}, {'prenom': 'Maryse', 'nom': 'Lefèvre', 'note': 9.5, 'rang': 46}, {'prenom': 'Georges', 'nom': 'Bouchet-Étienne', 'note': 7.5, 'rang': 87}, {'prenom': 'Susanne', 'nom': 'Barthelemy-Thomas', 'note': 9.5, 'rang': 46}, {'prenom': 'Thibaut', 'nom': 'Jacques', 'note': 11.5, 'rang': 14}, {'prenom': 'Adrienne', 'nom': 'Marin', 'note': 9.5, 'rang': 46}, {'prenom': 'Jeannine-Anne', 'nom': 'Collin', 'note': 7.0, 'rang': 92}, {'prenom': 'Bertrand', 'nom': 'Labbé', 'note': 9.5, 'rang': 46}, {'prenom': 'Hortense', 'nom': 'Lamy', 'note': 8.5, 'rang': 67}, {'prenom': 'Hélène', 'nom': 'Rivière', 'note': 10.0, 'rang': 39}, {'prenom': 'Éléonore-Bernadette', 'nom': 'Diaz', 'note': 10.5, 'rang': 25}, {'prenom': 'Laetitia', 'nom': 'Wagner', 'note': 11.5, 'rang': 14}, {'prenom': 'Honoré', 'nom': 'Tanguy-Royer', 'note': 10.5, 'rang': 25}, {'prenom': 'Inès', 'nom': 'Rousset', 'note': 9.5, 'rang': 46}, {'prenom': 'Rémy', 'nom': 'Rousset', 'note': 10.5, 'rang': 25}, {'prenom': 'René', 'nom': 'du Rossi', 'note': 12.5, 'rang': 4}, {'prenom': 'Valentine', 'nom': 'Vallet', 'note': 9.5, 'rang': 46}, {'prenom': 'Sébastien', 'nom': 'de la Laroche', 'note': 8.5, 'rang': 67}, {'prenom': 'Bernard-Xavier', 'nom': 'Mace', 'note': 10.0, 'rang': 39}, {'prenom': 'Susan', 'nom': 'Paul', 'note': 13.0, 'rang': 2}, {'prenom': 'Jules-Louis', 'nom': 'Lefebvre', 'note': 9.5, 'rang': 46}, {'prenom': 'Yves', 'nom': 'Marty-Thomas', 'note': 9.5, 'rang': 46}, {'prenom': 'André', 'nom': 'de la Hebert', 'note': 12.0, 'rang': 7}, {'prenom': 'Xavier', 'nom': 'Da Costa', 'note': 8.0, 'rang': 77}, {'prenom': 'Adrienne', 'nom': 'Le Valette', 'note': 10.0, 'rang': 39}, {'prenom': 'Bertrand', 'nom': 'Boulay', 'note': 12.0, 'rang': 7}, {'prenom': 'Célina', 'nom': 'Hoareau', 'note': 10.5, 'rang': 25}, {'prenom': 'Patricia', 'nom': 'Arnaud', 'note': 13.0, 'rang': 2}, {'prenom': 'William', 'nom': 'Gilbert-Pichon', 'note': 10.5, 'rang': 25}, {'prenom': 'Mathilde', 'nom': 'Gomez Le Boucher', 'note': 8.5, 'rang': 67}, {'prenom': 'Madeleine', 'nom': 'Vallet-Hervé', 'note': 10.0, 'rang': 39}, {'prenom': 'Margaret', 'nom': 'du Jean', 'note': 9.0, 'rang': 61}, {'prenom': 'Michelle', 'nom': 'Aubert', 'note': 6.5, 'rang': 97}, {'prenom': 'Dorothée-Patricia', 'nom': 'Salmon', 'note': 14.5, 'rang': 1}, {'prenom': 'Antoine-Thibaut', 'nom': 'Lesage', 'note': 12.0, 'rang': 7}, {'prenom': 'Marie', 'nom': 'Neveu', 'note': 11.0, 'rang': 19}, {'prenom': 'Stéphane', 'nom': 'Fournier', 'note': 12.0, 'rang': 7}, {'prenom': 'Louis', 'nom': 'Legrand', 'note': 8.0, 'rang': 77}, {'prenom': 'Céline', 'nom': 'Germain', 'note': 11.5, 'rang': 14}, {'prenom': 'Arnaude', 'nom': 'de Masse', 'note': 8.5, 'rang': 67}, {'prenom': 'Margaret', 'nom': 'Bousquet', 'note': 9.0, 'rang': 61}, {'prenom': 'Amélie', 'nom': 'de la Mercier', 'note': 8.5, 'rang': 67}, {'prenom': 'Patricia', 'nom': 'Maillard-Toussaint', 'note': 11.5, 'rang': 14}, {'prenom': 'Olivier', 'nom': 'Ollivier', 'note': 6.5, 'rang': 97}, {'prenom': 'Alexandre', 'nom': 'Garnier de la Coste', 'note': 8.0, 'rang': 77}, {'prenom': 'Thibault', 'nom': 'Bourdon', 'note': 9.0, 'rang': 61}, {'prenom': 'Amélie', 'nom': 'Lopes-Guyon', 'note': 8.5, 'rang': 67}, {'prenom': 'Roger', 'nom': 'Breton-Pascal', 'note': 8.5, 'rang': 67}, {'prenom': 'Sébastien', 'nom': 'Benoit-Muller', 'note': 10.5, 'rang': 25}, {'prenom': 'Alice', 'nom': 'Carlier', 'note': 10.5, 'rang': 25}, {'prenom': 'Claude', 'nom': 'Bernard', 'note': 10.0, 'rang': 39}, {'prenom': 'Antoinette', 'nom': 'Humbert', 'note': 11.0, 'rang': 19}, {'prenom': 'Lucas', 'nom': 'Brun-Lévêque', 'note': 9.5, 'rang': 46}, {'prenom': 'Zacharie', 'nom': 'Torres', 'note': 7.5, 'rang': 87}, {'prenom': 'Antoinette', 'nom': 'Brunel', 'note': 7.0, 'rang': 92}, {'prenom': 'Lucy', 'nom': 'Delorme de Bailly', 'note': 8.5, 'rang': 67}, {'prenom': 'Maurice', 'nom': 'Perret', 'note': 9.5, 'rang': 46}, {'prenom': 'Alexandrie', 'nom': 'Robert', 'note': 9.5, 'rang': 46}, {'prenom': 'Eugène', 'nom': 'Morel', 'note': 12.0, 'rang': 7}, {'prenom': 'Véronique', 'nom': 'Gaudin-Leleu', 'note': 7.5, 'rang': 87}, {'prenom': 'Denis', 'nom': 'Moreau', 'note': 8.0, 'rang': 77}, {'prenom': 'Océane', 'nom': 'Payet', 'note': 8.5, 'rang': 67}, {'prenom': 'Philippe', 'nom': 'Evrard-Benard', 'note': 12.0, 'rang': 7}, {'prenom': 'Nathalie', 'nom': 'Lopez', 'note': 6.5, 'rang': 97}, {'prenom': 'Bertrand', 'nom': 'Collin', 'note': 10.5, 'rang': 25}, {'prenom': 'Franck', 'nom': 'Courtois', 'note': 8.0, 'rang': 77}, {'prenom': 'Louis', 'nom': 'Goncalves', 'note': 10.0, 'rang': 39}, {'prenom': 'Édouard', 'nom': 'Bodin', 'note': 10.5, 'rang': 25}, {'prenom': 'Alix', 'nom': 'Maury', 'note': 8.0, 'rang': 77}, {'prenom': 'Adrien', 'nom': 'du Marchand', 'note': 12.5, 'rang': 4}, {'prenom': 'Isabelle', 'nom': 'Guillet de Duhamel', 'note': 9.5, 'rang': 46}, {'prenom': 'Alain', 'nom': 'Cohen', 'note': 12.5, 'rang': 4}, {'prenom': 'Richard', 'nom': 'Blot de la Clément', 'note': 10.5, 'rang': 25}, {'prenom': 'Adèle-Jacqueline', 'nom': 'Roy', 'note': 8.5, 'rang': 67}, {'prenom': 'Raymond', 'nom': 'Nicolas de Prévost', 'note': 12.0, 'rang': 7}, {'prenom': 'Renée', 'nom': 'de Bernier', 'note': 10.0, 'rang': 39}, {'prenom': 'Benoît', 'nom': 'Blanchard', 'note': 9.5, 'rang': 46}, {'prenom': 'Agnès', 'nom': 'de la Coste', 'note': 8.0, 'rang': 77}]
5) Écrivez une fonction qui enregistre les données de la liste ee
dans un fichier examen_cobol.csv
, avec une ligne par étudiant, comprenant les champs NOM
, Prénom
, note
, rang
, séparés par des tabulations, triés par ordre alphabétique des noms :
$ cat examen_cobol.csv
ALLAIN Patricia-Josette 9.0 61
ARNAUD Patricia 13.0 2
...
TORRES Zacharie 7.5 87
VALLET Valentine 9.5 46
VALLET-HERVÉ Madeleine 10.0 39
WAGNER Laetitia 11.5 14
def enregistre(ee, fichier):
ll = sorted(ee, key=lambda x:x['nom'].upper())
mm = [e['nom'].upper()+'\t'+e['prenom']+'\t'+str(e['note'])+'\t'+str(e['rang'])+'\n' for e in ll]
with open(fichier,'a') as f: f.writelines(mm)
enregistre(ee, 'examen_cobol.csv')
! ls -l examen_cobol.csv
-rw-r--r-- 1 jyt institut 5224 nov. 28 13:33 examen_cobol.csv
for e in sorted(ee, key=lambda x:x['nom'].upper()):
print(e['rang'], ' ', e['nom'].upper(), ' ', e['prenom'], ' ', e['note'])
61 ALLAIN Patricia-Josette 9.0 2 ARNAUD Patricia 13.0 97 AUBERT Michelle 6.5 97 BARON DE LOPEZ Alain 6.5 46 BARTHELEMY-THOMAS Susanne 9.5 25 BENOIT-MULLER Sébastien 10.5 39 BERNARD Claude 10.0 77 BIGOT David 8.0 46 BLANCHARD Benoît 9.5 25 BLOT DE LA CLÉMENT Richard 10.5 25 BODIN Édouard 10.5 87 BOUCHET-ÉTIENNE Georges 7.5 77 BOULANGER DU MUNOZ Margaret 8.0 7 BOULAY Bertrand 12.0 77 BOURDON Noël 8.0 61 BOURDON Thibault 9.0 61 BOUSQUET Margaret 9.0 67 BRETON-PASCAL Roger 8.5 46 BRUN-LÉVÊQUE Lucas 9.5 92 BRUNEL Antoinette 7.0 25 CARLIER Alice 10.5 46 CHARTIER Aimé 9.5 4 COHEN Alain 12.5 92 COLLIN Jeannine-Anne 7.0 25 COLLIN Bertrand 10.5 77 COURTOIS Franck 8.0 92 COUTURIER Thibault 7.0 77 DA COSTA Xavier 8.0 39 DE BERNIER Renée 10.0 77 DE LA COSTE Agnès 8.0 19 DE LA GONZALEZ Augustin 11.0 7 DE LA HEBERT André 12.0 67 DE LA LAROCHE Sébastien 8.5 67 DE LA MERCIER Amélie 8.5 67 DE MASSE Arnaude 8.5 14 DELAHAYE Antoine 11.5 67 DELORME DE BAILLY Lucy 8.5 46 DESCHAMPS DU HUMBERT Thibault 9.5 25 DIAZ Éléonore-Bernadette 10.5 61 DU JEAN Margaret 9.0 4 DU MARCHAND Adrien 12.5 4 DU ROSSI René 12.5 7 EVRARD-BENARD Philippe 12.0 25 FOUQUET Lucie 10.5 7 FOURNIER Stéphane 12.0 77 GARNIER DE LA COSTE Alexandre 8.0 87 GAUDIN-LELEU Véronique 7.5 19 GEORGES Clémence 11.0 14 GERMAIN Céline 11.5 25 GILBERT-PICHON William 10.5 19 GIRARD Margaud 11.0 67 GOMEZ LE BOUCHER Mathilde 8.5 39 GONCALVES Louis 10.0 46 GUILLET DE DUHAMEL Isabelle 9.5 25 HOAREAU Célina 10.5 19 HUMBERT Antoinette 11.0 14 JACQUES Thibaut 11.5 25 KLEIN Aimé-Sébastien 10.5 46 LABBÉ Bertrand 9.5 67 LAMY Hortense 8.5 39 LE VALETTE Adrienne 10.0 46 LEFEBVRE Jules-Louis 9.5 46 LEFÈVRE Maryse 9.5 77 LEGRAND Louis 8.0 7 LESAGE Antoine-Thibaut 12.0 67 LOPES-GUYON Amélie 8.5 97 LOPEZ Nathalie 6.5 39 MACE Bernard-Xavier 10.0 14 MAILLARD-TOUSSAINT Patricia 11.5 87 MARCHAND Vincent 7.5 46 MARIN Adrienne 9.5 46 MARTY-THOMAS Yves 9.5 92 MARY-GÉRARD Édouard 7.0 77 MAURY Alix 8.0 77 MOREAU Denis 8.0 7 MOREL Eugène 12.0 19 MORVAN Benoît 11.0 19 NEVEU Marie 11.0 7 NICOLAS DE PRÉVOST Raymond 12.0 87 NORMAND Gabriel 7.5 97 OLLIVIER Olivier 6.5 2 PAUL Susan 13.0 67 PAYET Océane 8.5 46 PERRET Maurice 9.5 61 PERRIN Alix 9.0 25 PHILIPPE DE VAILLANT Lucy 10.5 25 PIRES Christine 10.5 92 POULAIN Denis-Émile 7.0 39 RIVIÈRE Hélène 10.0 46 ROBERT Alexandrie 9.5 61 ROBIN Alfred 9.0 46 ROUSSET Inès 9.5 25 ROUSSET Rémy 10.5 67 ROY Adèle-Jacqueline 8.5 1 SALMON Dorothée-Patricia 14.5 25 TANGUY-ROYER Honoré 10.5 87 TORRES Zacharie 7.5 46 VALLET Valentine 9.5 39 VALLET-HERVÉ Madeleine 10.0 14 WAGNER Laetitia 11.5
Écrire un générateur Python qui prend comme entrée un flot (un autre générateur, une séquence) numérique, sans longueur spécifiée. La séquence engendrée montre la « tendance » des données source : si les valeurs des données augmentent, le résultat est +1, si diminuent : -1, si égaux : zéro. Il faut que le générateur construit mémorise le dernier élément lu depuis la source. Ainsi, si la séquence source est : 3, 3, 6, -2, 7, 8, 4, 2, 7, 7, 2, 6, 1, 2, 2, 6, ..., le résultat engendré par le générateur doit être : 0, 1, -1, 1, 1, -1, -1, 1, 0, -1, 1, -1, 1, 0, 1, .
>>> bla = iter([ 3, 3, 6, -2, 7, 8, 4, 2, 7, 7, 2, 6, 1, 2, 2, 6])
>>>f(bla)
<generator object f at 0x7fd4fbe33ac0>
>>> list(_)
[0, 1, -1, 1, 1, -1, -1, 1, 0, -1, 1, -1, 1, 0, 1]
def f(g):
a = next(g)
while 1:
try:
b = next(g); x = b-a
if x>0: yield 1
elif x<0: yield -1
else: yield 0
a = b
except StopIteration: break
bla = iter([ 3, 3, 6, -2, 7, 8, 4, 2, 7, 7, 2, 6, 1, 2, 2, 6])
f(bla)
<generator object f at 0x7ff8fbe3e900>
list(_)
[0, 1, -1, 1, 1, -1, -1, 1, 0, -1, 1, -1, 1, 0, 1]
Les fonctions suivantes sont à écrire en une ligne. On pourra préalablement importer un ou plusieurs modules.
1) Ecrire une fonction qui prend en entrée une liste et renvoie une liste de couples (x,n)
où n
est le nombre d'entrées consécutives égales à x
.
>>> ll = [2,2,2,2,1,1,2,2,1,1,1,4,4,4,4,2]
>>> f(ll)
[(2, 4), (1, 2), (2, 2), (1, 3), (4, 4), (2, 1)]
ll = [2,2,2,2,1,1,2,2,1,1,1,4,4,4,4,2]
from itertools import groupby
def f(ll): return [(x[0],len(list(x[1]))) for x in groupby(ll)]
f(ll)
[(2, 4), (1, 2), (2, 2), (1, 3), (4, 4), (2, 1)]
2) Ecrire une fonction qui prend en entréee une liste d'entiers et renvoie la liste réarrangée de manière à ce que les entiers impairs soient au début, et les pairs à la fin, en respectant leur ordre initial.
>>> mm = [38, 25, 36, 36, 40, 22, 8, 26, 32, 1, 7, 31, 46, 10, 21, 41, 47, 23, 24, 22, 5, 42, 41, 24, 36, 20, 27, 46, 6, 25]
>>> g(mm)
[25, 1, 7, 31, 21, 41, 47, 23, 5, 41, 27, 25, 38, 36, 36, 40, 22, 8, 26, 32, 46, 10, 24, 22, 42, 24, 36, 20, 46, 6]
mm = [38, 25, 36, 36, 40, 22, 8, 26, 32, 1, 7, 31, 46, 10, 21, 41, 47, 23, 24, 22, 5, 42, 41, 24, 36, 20, 27, 46, 6, 25]
def g(mm):
return [x for x in mm if x%2]+[x for x in mm if not x%2]
print(g(mm))
[25, 1, 7, 31, 21, 41, 47, 23, 5, 41, 27, 25, 38, 36, 36, 40, 22, 8, 26, 32, 46, 10, 24, 22, 42, 24, 36, 20, 46, 6]
3) Ecrire une fonction is_stricly_increasing
qui teste si une liste est strictement croissante.
On donnera deux versions, une itérative et une récursive.
>>> is_stricly_increasing([1,3,5,5,8,9,9])
False
>>> is_strictly_increasing_rec([2,4,5,9,15,18,25,51])
True
def is_stricly_increasing(ll): return all([ll[i]<ll[i+1] for i in range(len(ll)-1)])
is_stricly_increasing([1,3,5,5,8,9,9])
False
def is_strictly_increasing_rec(ll):
return len(ll)<2 or ll[0]<ll[1] and is_strictly_increasing_rec(ll[1:])
is_strictly_increasing_rec([2,4,5,9,15,18,25,51])
True