{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapitre 5 : \"Fonctions et espaces de nommage\" - solutions\n",
    "\n",
    "---\n",
    "\n",
    "<table class=\"appbclp1\">\n",
    "<tr> <td style=\"font-weight: bold!;\">auteurs :</td><td style=\"text-align: left!;\">Laurent Pointal &lt;<a href=\"mailtp:laurent.pointal@limsi.fr\">laurent.pointal@limsi.fr</a>&gt; &amp; Bob Cordeau &lt;<a href=\"pycours@kordeo.eu\">pycours@kordeo.eu</a>&gt; </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">licence :</td><td style=\"text-align: left!;\">Creative Commons Attribution-ShareAlike 4.0 International Public License (CC BY-SA 4.0) </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">source :</td><td style=\"text-align: left!;\"><a href=\"https://github.com/lpointal/appbclp\">https://github.com/lpointal/appbclp</a></td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">description :</td><td style=\"text-align: left!;\">Les programmes dans ce Notebook Jupyter servent de support au livre <span style=\"font-style: italic!;\">« Python 3 - Apprendre à  programmer dans l'écosystème Python»</span>, édition Dunod, dans lequel les différentes sections sont expliquées </td> </tr>\n",
    "<tr> <td style=\"font-weight: bold!;\">url :</td><td style=\"text-align: left!;\"><a href=\"https://www.dunod.com/sciences-techniques/python-3\">https://www.dunod.com/EAN/9782100809141</a></td> </tr>\n",
    "</table>\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_01\n",
    "\n",
    "# fonctions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def f1(nbre):\n",
    "    global x\n",
    "    x = nbre\n",
    "    print(\"Dans f1 :\", x)\n",
    "\n",
    "def f2(nbre):\n",
    "    global x\n",
    "    x = nbre\n",
    "    print(\"Dans f2 :\", x)\n",
    "    return x\n",
    "\n",
    "# programme principal ===============================================\n",
    "x = 2\n",
    "f1(40)\n",
    "print(\"Après f1 :\", x)\n",
    "x = f2(5)\n",
    "print(\"Après f2:\", x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print ('\\nIdem, mais sans \"global\"')\n",
    "\n",
    "# fonctions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def f1(nbre):\n",
    "    #global x\n",
    "    x = nbre\n",
    "    print(\"Dans f1 :\", x)\n",
    "\n",
    "def f2(nbre):\n",
    "    #global x\n",
    "    x = nbre\n",
    "    print(\"Dans f2 :\", x)\n",
    "    return x\n",
    "\n",
    "# programme principal ===============================================\n",
    "x = 2\n",
    "f1(40)\n",
    "print(\"Après f1 :\", x)\n",
    "x = f2(5)\n",
    "print(\"Après f2:\", x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_02\n",
    "\n",
    "# fonctions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def maximum(n1, n2, n3):\n",
    "    \"\"\"Renvoie le plus grand de trois nombres.\"\"\"\n",
    "    if (n1 >= n2) and (n1 >= n3):\n",
    "        return n1\n",
    "    elif (n2 >= n1) and (n2 >= n3):\n",
    "        return n2\n",
    "    else:\n",
    "        return n3\n",
    "    # solution alternative :\n",
    "    return max(n1, n2, n3)\n",
    "\n",
    "# programme principal ===============================================\n",
    "print (maximum(4.5, 5.7, 3.9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_03\n",
    "\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def bornes(liste):\n",
    "    \"\"\"Renvoie les bornes mini et maxi d'une liste.\"\"\"\n",
    "    return min(liste), max(liste)\n",
    "\n",
    "# programme principal ===============================================\n",
    "print (bornes([4.5, 5.7, 3.9, 3.5, 2.4, 4.8]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_04\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def indexMax(tt):\n",
    "    \"\"\"Renvoie l'indice du plus grand élément de la liste <tt>.\"\"\"\n",
    "    i, maxi = 0, min(seq)\n",
    "    while i < len(tt):\n",
    "        if tt[i] > maxi:\n",
    "            maxi, imax = tt[i], i\n",
    "        i = i + 1\n",
    "    return imax\n",
    "    # solution alternative :\n",
    "    return tt.index(max(tt))\n",
    "\n",
    "# programme principal ===============================================\n",
    "seq = [5, 8, 2, 1, 9, 3, 6, 7]\n",
    "print(indexMax(seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_05\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def indexMinMax(tt):\n",
    "    \"\"\"Renvoie les indice du plus petit et du plus grand élément de la liste <tt>.\"\"\"\n",
    "    i, mini, maxi, lng = 0, max(seq), min(seq), len(tt)\n",
    "    while i < lng:\n",
    "        if tt[i] < mini:\n",
    "            mini, imin = tt[i], i\n",
    "        if tt[i] > maxi:\n",
    "            maxi, imax = tt[i], i\n",
    "        i = i + 1\n",
    "    return imin, imax\n",
    "    # solution alternative :\n",
    "    return tt.index(min(tt)), tt.index(max(tt))\n",
    "\n",
    "\n",
    "# programme principal ===============================================\n",
    "seq = [5, 8, 2, 1, 9, 3, 6, 7]\n",
    "print(indexMinMax(seq))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_06"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_06\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def filtrage(un_tuple):\n",
    "    \"\"\"Renvoie une liste d’éléments de <un_tuple> qui commencent par \"anti\".\"\"\"\n",
    "    res = []\n",
    "    for un_element in un_tuple :\n",
    "        if un_element.startswith(\"anti\"):\n",
    "            res.append(un_element)\n",
    "    return res\n",
    "\n",
    "# programme principal ===============================================\n",
    "mon_tuple = (\"proconsul\",\"antimafia\",\"repas\",\"antipasti\",\"ex-aequo\", \"antichar\")\n",
    "print(\"Resultat du filtrage :\", filtrage(mon_tuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_07"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_07\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def cinqErreurs(): # oubli des ':'\n",
    "    x = range(41)  # affectation : '='\n",
    "\n",
    "    for i in x:\n",
    "        if (i % 7) == 0:     # test : '=='. si modulo 7 donne 0\n",
    "            if (i % 4) != 0: # oubli du '#'. si modulo 4 est different de 0\n",
    "                print (f\"{i} est un multiple de 7\")\n",
    "            else:            # else et non pas elif\n",
    "                print (f\"{i} est un multiple de 7 et de 4\")\n",
    "  \n",
    "        elif (i % 4) == 0:   # Si modulo par 4 donne 0\n",
    "            print (f\"{i} est un multiple de 4\")\n",
    "  \n",
    "# programme principal ===============================================\n",
    "cinqErreurs()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_08"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_08\n",
    "\n",
    "# fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def PGCD(x, y):\n",
    "    \"\"\"algorithme d'Euclide\"\"\"\n",
    "    while y:\n",
    "        z, x = x, y\n",
    "        y = z % y\n",
    "    return x\n",
    "\n",
    "def PPCM(x, y):\n",
    "    \"\"\"conséquence de l'algorithme du PGCD\"\"\"\n",
    "    return abs(x * y) / PGCD(x, y)\n",
    "\n",
    "# programme principal ===============================================\n",
    "print (PGCD(6, 27))  # 3\n",
    "print (PPCM(6, 27))  # 54"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_09\n",
    "\n",
    "# fonctions\n",
    "def indexMax(tt):\n",
    "    \"\"\"Renvoie l'indice du plus grand element de la liste <tt>.\"\"\" # Indentation!\n",
    "    i, max = 0, 0\n",
    "    while i < len(tt):        # ; au lien de :\n",
    "        if tt[i] > max :\n",
    "            max, imax = tt[i], i\n",
    "        i = i + 1             # Increment indente dans mauvais bloc (if)\n",
    "    return imax               # Return indente dans mauvais bloc (while)\n",
    "\n",
    "# programme principal -----------------------------------------------\n",
    "serie = [5, 8, 2, 1, 9, 3, 6, 7]\n",
    "print(indexMax(serie))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_10\n",
    "\n",
    "# fonctions\n",
    "def padi(ch):\n",
    "    \"\"\"Renvoie 'True' si la chaine <ch> ne contient pas la lettre 'i',\n",
    "    'False' sinon.\"\"\"\n",
    "    print(\">> padi\", ch)\n",
    "    res = 'i' not in ch\n",
    "    print(\"<< padi\", ch, res)\n",
    "    return res\n",
    "\n",
    "def pado(ch):\n",
    "    \"\"\"Renvoie 'True' si la chaine <ch> ne contient pas la lettre 'o',\n",
    "    'False' sinon.\"\"\"\n",
    "    print(\">> pado\", ch)\n",
    "    res = 'o' not in ch\n",
    "    print(\"<< pado\", ch, res)\n",
    "    return res\n",
    "\n",
    "def padiPado(ch):\n",
    "    \"\"\"Renvoie 'True' si la chaine <ch> ne contient pas les lettres 'i' et 'o',\n",
    "    'False' sinon.\"\"\"\n",
    "    print(\">> padiPado\", ch)\n",
    "    res = padi(ch) and pado(ch)\n",
    "    print(\"<< padiPado\", ch, res)\n",
    "    return res\n",
    "\n",
    "# programme principal -----------------------------------------------\n",
    "print ('padi(\"poire\") ==>', padi(\"poire\"))\n",
    "print ('pado(poire\") ==>', pado(\"poire\"))\n",
    "print ('padiPado(\"banane\") ==>', padiPado(\"banane\"))\n",
    "print ('padiPado(\"brugnon\") ==>', padiPado(\"brugnon\"))\n",
    "print ('padiPado(\"poire\") ==>', padiPado(\"poire\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "On remarque l'évaluation *shorcut* (au plus court) avec le cas `padiPado(\"poire\")`, où l'évaluation à `False` par `padi()` suffit pour avoir le résultat de l'expression booléenne, ce qui entraîne que l'appel à `pado()` est superflu et n'est pas effectué."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_11\n",
    "\n",
    "# fonctions\n",
    "def dansRectangle(rectangle, point):\n",
    "    \"\"\"Teste si le <point> est dans le <rectangle>.\"\"\"\n",
    "    Xw = rectangle[0][0]\n",
    "    Ys = rectangle[0][1]\n",
    "    Xe = rectangle[1][0]\n",
    "    Yn = rectangle[1][1]\n",
    "    X = point[0]\n",
    "    Y = point[1]\n",
    "    return True if (Xw<=X<=Xe) and (Ys<=Y<=Yn) else False\n",
    "\n",
    "# programme principal -----------------------------------------------\n",
    "rect = ((-1.0, -1.0), (2.0, 3.0))\n",
    "pt = (1.0, 1.0)\n",
    "print (dansRectangle(rect, pt))\n",
    "pt = (3.0, 1.0)\n",
    "print (dansRectangle(rect, pt))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "L'utilisation de tuples immutables comme paramètres assure que les valeurs transmises ne seront pas modifiées par la fonction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_12\n",
    "\n",
    "**La permutation**\n",
    "\n",
    "Le but est de travailler sur des échanges de position entre les chiffres. Bien qu'il soit possible d'accéder à chacun d'eux en utilisant la division entière et l'opérateur modulo, le plus simple est de ne les considérer, pour la permutation, que comme des symboles et donc de passer par la *représentation sous forme de chaîne* des nombres. On repassera sous forme numérique lorsque nous aurons à nous intéresser aux valeurs elles-mêmes. \n",
    "Par rapport à l'algorithme, Il faudra faire attention à ce que la représentation comporte toujours 4 chiffres, quitte à compléter avec des zéros en tête.\n",
    "\n",
    "**Vérification de la saisie**\n",
    "\n",
    "Pour avoir 4 chiffres, il suffit de vérifier que le nombre est entre 1000 inclus et 10000 exclus. Pour être sûr que les chiffres ne soient pas tous les mêmes, on peut vérifier que le nombre n'est pas dans la liste des 9 (on exclus le mombre 0000) nombres de quatre chiffres identiques.\n",
    "\n",
    "**Programme principal**\n",
    "\n",
    "Après une saisie filtrée dans la fonction `entiert4()`, on va simplement appliquer l'algorithme donné (en ajoutant un comptage du nombre d'itérations où un nouveau nombre est calculé, afin de contrôler la vitesse d'acquisition du nombre de Kaprekar)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5593 => 5994 => 5355 => 1998 => 8082 => 8532 => 6174\b\b\b\b\b *** 6174 ***\n",
      "En 6 coups\n"
     ]
    }
   ],
   "source": [
    "# exo05_12\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from random import seed, randrange\n",
    "from itertools import permutations\n",
    "\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def entier4():\n",
    "    \"\"\"Retourne un entier de 4 chiffres pas tous égaux.\"\"\"\n",
    "    \n",
    "    TOUS_EGAUX = (1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999)\n",
    "    seed()\n",
    "    n = randrange(1000, 10000)\n",
    "    while n in TOUS_EGAUX:\n",
    "        n = randrange(1000, 10000)\n",
    "    return n\n",
    "    \n",
    "    \n",
    "def ch4(n):\n",
    "    \"\"\"Retourne une chaîne de 4 chiffres de n.\"\"\"\n",
    "    \n",
    "    s = str(n)\n",
    "    while len(s) < 4:\n",
    "        s = '0' + s\n",
    "    return s\n",
    "\n",
    "\n",
    "def ch2nb4(ch, sens_direct=True):\n",
    "    \"\"\"Retourne un entier de 4 chiffres à partir d'une liste de ses items.\"\"\"\n",
    "\n",
    "    if sens_direct:\n",
    "        return int(ch[0])*1000 + int(ch[1])*100 + int(ch[2])*10 + int(ch[3])\n",
    "    else:   # génère PG en lisant PP de droite à gauche\n",
    "        return int(ch[0]) + int(ch[1])*10 + int(ch[2])*100 + int(ch[3])*1000\n",
    "    \n",
    "\n",
    "# Programme principal ==========================================================\n",
    "    \n",
    "# On génère un entier au hasard\n",
    "nb4 = entier4()\n",
    "print(nb4, end='')\n",
    "    \n",
    "# On applique l'algorithme\n",
    "nbCoup = 0\n",
    "while nb4 != 6174:\n",
    "    nbCoup += 1\n",
    "    PP = 10000\n",
    "    for i in permutations(ch4(nb4), 4):\n",
    "        n_i = ch2nb4(i)\n",
    "        if n_i < PP:\n",
    "            PP = n_i\n",
    "            PPi = i\n",
    "    PG = ch2nb4(PPi, False)\n",
    "    nb4 = PG - PP\n",
    "    print(\" =>\", nb4, end='')\n",
    "    \n",
    "print(\"{} *** 6174 ***\".format('\\b'*5))\n",
    "if nbCoup == 1:\n",
    "    print(\"En 1 coup !\")\n",
    "else:\n",
    "    print(\"En\", nbCoup, \"coups\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_13\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from random import seed, randrange\n",
    "from itertools import permutations\n",
    "\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def entier3():\n",
    "    \"\"\"Retourne un entier de 3 chiffres pas tous égaux.\"\"\"\n",
    "    \n",
    "    TOUS_EGAUX = (111, 222, 333, 444, 555, 666, 777, 888, 999)\n",
    "    seed()\n",
    "    n = randrange(100, 1000)\n",
    "    while n in TOUS_EGAUX:\n",
    "        n = randrange(100, 1000)\n",
    "    return n\n",
    "    \n",
    "    \n",
    "def ch3(n):\n",
    "    \"\"\"Retourne une chaîne d'exactement 3 chiffres de n.\"\"\"\n",
    "    \n",
    "    s = str(n)\n",
    "    while len(s) < 3:\n",
    "        s = '0' + s\n",
    "    return s\n",
    "\n",
    "\n",
    "def ch2nb3(ch, sens_direct=True):\n",
    "    \"\"\"Retourne un entier de 3 chiffres à partir d'une liste de ses items.\"\"\"\n",
    "\n",
    "    if sens_direct:\n",
    "        return int(ch[0])*100 + int(ch[1])*10 + int(ch[2])\n",
    "    else:   # génère PG en lisant PP de droite à gauche\n",
    "        return int(ch[0]) + int(ch[1])*10 + int(ch[2])*100\n",
    "    \n",
    "\n",
    "# Programme principal =========================================================\n",
    "    \n",
    "# On génère un entier au hasard\n",
    "nb3 = entier3()\n",
    "print(nb3, end='')\n",
    "    \n",
    "# On applique l'algorithme\n",
    "nbCoup = 0\n",
    "while nb3 != 495:\n",
    "    nbCoup += 1\n",
    "    PP = 1000\n",
    "    for i in permutations(ch3(nb3), 3):\n",
    "        n_i = ch2nb3(i)\n",
    "        if n_i < PP:\n",
    "            PP = n_i\n",
    "            PPi = i\n",
    "    PG = ch2nb3(PPi, False)\n",
    "    nb3 = PG - PP\n",
    "    print(\" =>\", nb3, end='')\n",
    "    \n",
    "print(\"{} *** 495 ***\".format('\\b'*4))\n",
    "if nbCoup == 1:\n",
    "    print(\"En 1 coup !\")\n",
    "else:\n",
    "    print(\"En\", nbCoup, \"coups\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_14\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def carteRestante(n):\n",
    "    \"On retire la première carte, on met la 2e sous le paquet et on itère.\"\n",
    "    \n",
    "    # on crée le jeu (index de 0 à n-1 au début).\n",
    "    jeu = list(range(1, n+1))\n",
    "    \n",
    "    # on joue\n",
    "    while len(jeu) > 1:    # tant qu'il reste plus d'une carte\n",
    "        del jeu[0]         # on retire la 1ère carte\n",
    "        carte = jeu[0]     # on prend celle qui était derrière\n",
    "        del jeu[0]         # on la supprime du paquet\n",
    "        jeu.append(carte)  # et on la remet à la fin !\n",
    "\n",
    "    return jeu[0]           # on retourne la dernière carte\n",
    "\n",
    "# programme principal =========================================================\n",
    "taille = int(input('Taille du jeu de cartes : '))\n",
    "while taille < 2:\n",
    "    taille = int(input('Taille du jeu (taille > 1) s.v.p. : '))\n",
    "\n",
    "print('Carte restante :', carteRestante(taille))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "**Note :** On peut remarquer que si la taille du jeu est une puissance de 2 alors la carte restante est celle qui était à la fin du paquet d'origine."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_15\n",
    "\"\"\"\n",
    "    Exercice adapté du MOOC bioinformatique de François Rechenmann & Thierry Parmentelat (inria).\n",
    "    https://www.fun-mooc.fr/courses/course-v1:inria+41003+selfpaced/about\n",
    "\"\"\"\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def dans(s, autorises):\n",
    "    for c in s:  # on vérifie chaque caractère\n",
    "        if c not in autorises:\n",
    "            return False\n",
    "    # aucun caractère n'a posé de problème, la chaine est bonne\n",
    "    return True\n",
    "\n",
    "\n",
    "# Programme principal =========================================================\n",
    "aa = 'arndceqghilkmfpstwyv'\n",
    "\n",
    "sp = input('Entrez une séquence : ')\n",
    "\n",
    "print(\"C'est une séquence protéique :\", dans(sp, aa))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo05_16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo05_16\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def cdu(n):\n",
    "    \"\"\"Retourne le triplet (centaine, dizaine, unité).\"\"\"\n",
    "    c = n // 100\n",
    "    du = n - (c * 100)\n",
    "    d = du // 10\n",
    "    u = du - (d * 10)\n",
    "    \n",
    "    return c, d, u\n",
    "\n",
    "# Programme principal =========================================================\n",
    "for n in range(501, 1000, 2):\n",
    "    chiffres = cdu(n)\n",
    "    if chiffres[0] + chiffres[1] + chiffres[2] == 7:\n",
    "        if ((chiffres[0] == chiffres[1]) \n",
    "            or (chiffres[0] == chiffres[2])\n",
    "            or (chiffres[1] == chiffres[2])):\n",
    "                print(f\"Nombre mystère : {n}\")"
   ]
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
