.. _interrorapide20minutes201509rst: ================================================================ 1A.e - Correction de l’interrogation écrite du 26 septembre 2015 ================================================================ .. only:: html **Links:** :download:`notebook `, :downloadlink:`html `, :download:`python `, :downloadlink:`slides `, :githublink:`GitHub|_doc/notebooks/exams/interro_rapide_20_minutes_2015_09.ipynb|*` tests, boucles, fonctions .. code:: ipython3 from jyquickhelper import add_notebook_menu add_notebook_menu() .. contents:: :local: Enoncé 1 ~~~~~~~~ Q1 ^^ Le programme suivant provoque une erreur pourquoi ? .. code:: ipython3 tab = [1, 3] for i in range(0, len(tab)): print(tab[i] + tab[i+1]) .. parsed-literal:: 4 :: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () 1 tab = [1, 3] 2 for i in range(0, len(tab)): ----> 3 print(tab[i] + tab[i+1]) IndexError: list index out of range On découvre le problème en ajoutant des affichages intermédiaires : .. code:: ipython3 tab = [1, 3] for i in range(0, len(tab)): print(i, i+1, len(tab)) print(tab[i] + tab[i+1]) .. parsed-literal:: 0 1 2 4 1 2 2 :: --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () 2 for i in range(0, len(tab)): 3 print(i, i+1, len(tab)) ----> 4 print(tab[i] + tab[i+1]) IndexError: list index out of range A la dernière itération, :math:`i+1` dévient égal à la longueur de la liste ``tab`` or le dernier indice d’un tableau est ``len(tab)-1``. Q2 ^^ Où est l’erreur de syntaxe ? .. code:: ipython3 n = 1 if n = 1: y = 0 else: y = 1 :: File "", line 2 if n = 1: ^ SyntaxError: invalid syntax Le test d’égalité s’écrit ``==``. Q3 ^^ On associe la valeur 1 à la lettre *a*, 2 à *b* et ainsi de suite. Ecrire une fonction qui fait la somme de ces valeurs pour une chaîne de caractères. Exemple : elu :math:`\rightarrow` 5 + 12 + 21 = 38 .. code:: ipython3 def somme_caracteres(mot): s = 0 for c in mot : s += ord(c) - ord("a") + 1 return s somme_caracteres("elu") .. parsed-literal:: 38 On peut l’écrire de façon plus courte : .. code:: ipython3 def somme_caracteres(mot): return sum(ord(c) - ord("a") + 1 for c in mot) somme_caracteres("elu") .. parsed-literal:: 38 Enoncé 2 ~~~~~~~~ Q1 ^^ Barrez les lignes qui produiraient une erreur à l’exécution et dire pourquoi ? .. code:: ipython3 y = "a" * 3 + 1 z = 3 * "a" + 1 print(y,z) :: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 y = "a" * 3 + 1 2 z = 3 * "a" + 1 3 print(y,z) TypeError: Can't convert 'int' object to str implicitly Les deux premières lignes sont incorrects car on essaye d’ajouter une chaîne de caractères à un nombre. La première opération est correcte ``"a" * 3``. Dans un sens comme dans l’autre, elle donne ``"aaa"``. Mais on ne peut ajouter 1 à ``"aaa"``. Q2 ^^ Que vaut ``l`` à la fin du programme ? .. code:: ipython3 l = [] for i in range(0, 10): l.append([i]) print(l) .. parsed-literal:: [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]] Il ne faut pas confondre la méthode `append `__ et `extend `__. .. code:: ipython3 l = [] for i in range(0, 10): l.extend([i]) print(l) .. parsed-literal:: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Q3 ^^ Ecrire une fonction qui prend une chaîne de caractères et qui lui enlève une lettre sur 2. .. code:: ipython3 def un_sur_deux(mot): s = "" for i,c in enumerate(mot): if i % 2 == 0: s += c return s un_sur_deux("python") .. parsed-literal:: 'pto' Ou plus court encore : .. code:: ipython3 def un_sur_deux(mot): return "".join( c for i,c in enumerate(mot) if i % 2 == 0 ) un_sur_deux("python") .. parsed-literal:: 'pto'