{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# 1A.e - Correction de l'interrogation \u00e9crite du 26 septembre 2015\n", "\n", "tests, boucles, fonctions"]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/html": ["
run previous cell, wait for 2 seconds
\n", ""], "text/plain": [""]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["from jyquickhelper import add_notebook_menu\n", "add_notebook_menu()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Enonc\u00e9 1"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q1\n", "\n", "Le programme suivant provoque une erreur pourquoi ?"]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["4\n"]}, {"ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": ["\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mtab\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mtab\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: list index out of range"]}], "source": ["tab = [1, 3]\n", "for i in range(0, len(tab)):\n", " print(tab[i] + tab[i+1])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["On d\u00e9couvre le probl\u00e8me en ajoutant des affichages interm\u00e9diaires :"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["0 1 2\n", "4\n", "1 2 2\n"]}, {"ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": ["\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtab\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mtab\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mIndexError\u001b[0m: list index out of range"]}], "source": ["tab = [1, 3]\n", "for i in range(0, len(tab)):\n", " print(i, i+1, len(tab))\n", " print(tab[i] + tab[i+1])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["A la derni\u00e8re it\u00e9ration, $i+1$ d\u00e9vient \u00e9gal \u00e0 la longueur de la liste ``tab`` or le dernier indice d'un tableau est ``len(tab)-1``."]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q2\n", "\n", "O\u00f9 est l'erreur de syntaxe ?"]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [{"ename": "SyntaxError", "evalue": "invalid syntax (, line 2)", "output_type": "error", "traceback": ["\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m if n = 1:\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"]}], "source": ["n = 1\n", "if n = 1:\n", " y = 0\n", "else:\n", " y = 1"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Le test d'\u00e9galit\u00e9 s'\u00e9crit ``==``."]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q3\n", "\n", "On associe la valeur 1 \u00e0 la lettre *a*, 2 \u00e0 *b* et ainsi de suite. Ecrire une fonction qui fait la somme de ces valeurs pour une cha\u00eene de caract\u00e8res.\n", "\n", "Exemple : elu $\\rightarrow$ 5 + 12 + 21 = 38"]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"data": {"text/plain": ["38"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["def somme_caracteres(mot):\n", " s = 0\n", " for c in mot :\n", " s += ord(c) - ord(\"a\") + 1\n", " return s\n", "\n", "somme_caracteres(\"elu\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["On peut l'\u00e9crire de fa\u00e7on plus courte :"]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"data": {"text/plain": ["38"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["def somme_caracteres(mot):\n", " return sum(ord(c) - ord(\"a\") + 1 for c in mot)\n", "\n", "somme_caracteres(\"elu\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Enonc\u00e9 2"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q1\n", "\n", "Barrez les lignes qui produiraient une erreur \u00e0 l'ex\u00e9cution et dire pourquoi ?"]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [{"ename": "TypeError", "evalue": "Can't convert 'int' object to str implicitly", "output_type": "error", "traceback": ["\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"a\"\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m3\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mz\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m3\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;34m\"a\"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mz\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: Can't convert 'int' object to str implicitly"]}], "source": ["y = \"a\" * 3 + 1\n", "z = 3 * \"a\" + 1\n", "print(y,z)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Les deux premi\u00e8res lignes sont incorrects car on essaye d'ajouter une cha\u00eene de caract\u00e8res \u00e0 un nombre. La premi\u00e8re op\u00e9ration est correcte ``\"a\" * 3``. Dans un sens comme dans l'autre, elle donne ``\"aaa\"``. Mais on ne peut ajouter 1 \u00e0 ``\"aaa\"``."]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q2\n", "\n", "Que vaut ``l`` \u00e0 la fin du programme ?"]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]\n"]}], "source": ["l = []\n", "for i in range(0, 10):\n", " l.append([i])\n", "print(l)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Il ne faut pas confondre la m\u00e9thode [append](https://docs.python.org/3.4/tutorial/datastructures.html#more-on-lists) et [extend](https://docs.python.org/3.4/tutorial/datastructures.html#more-on-lists)."]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"name": "stdout", "output_type": "stream", "text": ["[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"]}], "source": ["l = []\n", "for i in range(0, 10):\n", " l.extend([i])\n", "print(l)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["#### Q3\n", "\n", "Ecrire une fonction qui prend une cha\u00eene de caract\u00e8res et qui lui enl\u00e8ve une lettre sur 2."]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [{"data": {"text/plain": ["'pto'"]}, "execution_count": 11, "metadata": {}, "output_type": "execute_result"}], "source": ["def un_sur_deux(mot):\n", " s = \"\"\n", " for i,c in enumerate(mot):\n", " if i % 2 == 0:\n", " s += c\n", " return s\n", "\n", "un_sur_deux(\"python\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Ou plus court encore :"]}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [{"data": {"text/plain": ["'pto'"]}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["def un_sur_deux(mot):\n", " return \"\".join( c for i,c in enumerate(mot) if i % 2 == 0 )\n", "\n", "un_sur_deux(\"python\")"]}, {"cell_type": "code", "execution_count": 12, "metadata": {"collapsed": true}, "outputs": [], "source": []}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1"}}, "nbformat": 4, "nbformat_minor": 2}