{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapitre 4 : \"Conteneurs standard\" - 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": [
    "### exo04_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# exo04_01\n",
    "\n",
    "liste = [2, 56, 104, 5, 2, 3, 2, 1]\n",
    "\n",
    "liste.sort()\n",
    "print(liste)\n",
    "\n",
    "liste.append(12)\n",
    "print(liste)\n",
    "\n",
    "liste.reverse()  # ou bien liste = liste[::-1]\n",
    "print(liste)\n",
    "\n",
    "print(liste.index(3))\n",
    "\n",
    "while 2 in liste:\n",
    "    liste.remove(2)\n",
    "print(liste)\n",
    "\n",
    "print(liste[1:4])\n",
    "\n",
    "print(liste[:2])\n",
    "\n",
    "print(liste[2:])\n",
    "\n",
    "print(liste[:])\n",
    "\n",
    "print(liste[-3:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_02\n",
    "\n",
    "liste1 = []        # ou bien liste1 = list()\n",
    "liste2 = [0.0]*5   # opérateur de répétition\n",
    "\n",
    "print('liste1 =', liste1)\n",
    "print('liste2 =', liste2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_03"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_03\n",
    "\n",
    "print(list(range(3)))\n",
    "print(list(range(2, 15, 2)))\n",
    "print(list(range(4)) + list(range(7, 12, 2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_04\n",
    "\n",
    "liste = []\n",
    "for n in range(21):\n",
    "    if n % 3 ==0:\n",
    "        liste.append(n)\n",
    "        \n",
    "print(\"liste =\", liste)        \n",
    "        \n",
    "for i in range(4, 11):\n",
    "    if i in liste:\n",
    "        print(\"\\t\", i, \"est dans\", liste)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_05\n",
    "\n",
    "liste = list(range(6))\n",
    "for i in range(len(liste)):\n",
    "    if liste[i] >= 2:\n",
    "        liste[i] += 3\n",
    "        \n",
    "print(liste)        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_06"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_06\n",
    "\n",
    "somme = 0\n",
    "for n in range(60):\n",
    "    somme += n\n",
    "# ou, plus rapide, utilisation de la fonction builtin sum()\n",
    "somme = sum(range(60))\n",
    "\n",
    "print(\"Somme =\", somme)    \n",
    "print(\"Vérification\", (59 * 60) // 2)  # somme = (n * (n+1)) / 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_07"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_07\n",
    "\n",
    "E = {'G', 'U', 'D', 'U', 'L', 'E'}\n",
    "F = {'G', 'E', 'L', 'I', 'N', 'O', 'T', 'T', 'E'}\n",
    "\n",
    "print(\"E :\", E)\n",
    "print(\"F :\", F)\n",
    "print(\"\\nCardinal de F =\", len(F))\n",
    "print(\"\\n'D' appartient à E :\", 'D' in E)\n",
    "print(\"'D' appartient à F :\", 'D' in F)\n",
    "print(\"\\nE\\F :\", E - F)\n",
    "print(\"F\\E :\", F - E)\n",
    "print(\"\\nE union F :\", E | F)\n",
    "print(\"E inter F :\", E & F)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_08"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_08\n",
    "\n",
    "elements = dict()  # ou bien elements = {}\n",
    "\n",
    "# remplissage du dictionnaire : on associe un tuple (Te, Tf, Z, A) à une clé\n",
    "elements['Au'] = (2970, 1063, 79, 196.967)  # or\n",
    "elements['Ga'] = (2237, 29.8, 31, 69.72)    # gallium\n",
    "\n",
    "print(\"Numéro atomique de l'or :\", elements[\"Au\"][2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_09\n",
    "\n",
    "liste = [19, 5, 8, 2, 13, 78, 1, 5]\n",
    "liste.sort()\n",
    "print(liste[::-1])\n",
    "# ou\n",
    "liste.sort(reverse=True)\n",
    "print(liste)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_10\n",
    "\n",
    "a = list(range(0, 19, 2))\n",
    "b = list(range(1, 21, 2))\n",
    "\n",
    "print(\"a =\", a)\n",
    "print(\"b =\", b)\n",
    "\n",
    "ab = list()\n",
    "for i in range(10):\n",
    "    ab.append(a[i])\n",
    "    ab.append(b[i])\n",
    "    \n",
    "print(\"ab =\", ab)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# exo04_11\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",
    "base_comp = {\n",
    "        'A': 'T',\n",
    "        'C': 'G',\n",
    "        'G': 'C',\n",
    "        'T': 'A'\n",
    "        }\n",
    "\n",
    "seq = 'GGACGGACGTTGACT'\n",
    "seq_c = ''\n",
    "\n",
    "for base in seq:\n",
    "    seq_c += base_comp[base]\n",
    "\n",
    "print('Séquence initiale       :', seq)\n",
    "print('Séquence complémentaire :', seq_c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### exo04_12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# exo04_12\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",
    "seq = 'lewyfhwrylps'\n",
    "aa_test = ['n', 'y']\n",
    "\n",
    "for c in aa_test:\n",
    "    rep = seq.find(c)\n",
    "    if rep < 0:\n",
    "        print(f\"'{c}' n'est pas dans '{seq}'\")\n",
    "    else:\n",
    "        print(f\"'{c}' est en position {rep = 1} dans '{seq}'\")"
   ]
  }
 ],
 "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
}
