{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 4 - 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": [
    "### Exercice 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 1\n",
    "\n",
    "list1, list2 = [1, 2, 3, 4], [5, 6]\n",
    "\n",
    "result = False\n",
    "for x in list1:\n",
    "    for y in list2:\n",
    "        if x == y:\n",
    "            result = True\n",
    "            break\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 2\n",
    "\n",
    "mots = ['abc', 'aba', 'xyz', '1221']\n",
    "\n",
    "print([mot for mot in mots if len(mot) > 1 and mot[0] == mot[-1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 3\n",
    "\n",
    "list1, list2 = [1, 2, 3, 4], [1, 2]\n",
    "\n",
    "print(list(set(list1) - set(list2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 4\n",
    "\n",
    "d = {0: 1, 1: 10, 2: 20}\n",
    "\n",
    "valeur = sum(d.values())\n",
    "d.update({'nouveau': valeur})\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 5\n",
    "\n",
    "n = int(input(\"Entrez un entier [2 .. 12] : \"))\n",
    "while not(n >= 2 and n <= 12):\n",
    "    n = int(input(\"Entrez un entier [2 .. 12], s.v.p. : \"))\n",
    "\n",
    "s = 0\n",
    "for i in range(1, 7):\n",
    "    for j in range(1, 7):\n",
    "        if i+j == n:\n",
    "            s += 1\n",
    "\n",
    "print(\"Il y a {:d} façon(s) de faire {:d} avec deux dés.\".format(s, n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 6\n",
    "\n",
    "n = int(input(\"Entrez un entier [3 .. 18] : \"))\n",
    "while not(n >= 3 and n <= 18):\n",
    "    n = int(input(\"Entrez un entier [3 .. 18], s.v.p. : \"))\n",
    "\n",
    "s = 0\n",
    "for i in range(1, 7):\n",
    "    for j in range(1, 7):\n",
    "        for k in range(1, 7):\n",
    "            if i+j+k == n:\n",
    "                s += 1\n",
    "\n",
    "print(\"Il y a {:d} façon(s) de faire {:d} avec trois dés.\".format(s, n))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 7\n",
    "\n",
    "# Globale ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "MAX = 8\n",
    "\n",
    "# Programme principal =========================================================\n",
    "nbd = int(input(\"Nombre de dés [2 .. {:d}] : \".format(MAX)))\n",
    "while not(nbd >= 2and nbd <= MAX):\n",
    "    nbd = int(input(\"Nombre de dés [2 .. {:d}], s.v.p. : \".format(MAX)))\n",
    "\n",
    "s = int(input(\"Entrez un entier [{:d} .. {:d}] : \".format(nbd, 6*nbd)))\n",
    "while not(s >= nbd and s <= 6*nbd):\n",
    "    s = int(input(\"Entrez un entier [{:d} .. {:d}], s.v.p. : \".format(nbd, 6*nbd)))\n",
    "\n",
    "if s == nbd or s == 6*nbd:\n",
    "    cpt = 1 # 1 seule solution\n",
    "else:\n",
    "    init = [1]*nbd      # initialise une liste de <nbd> dés\n",
    "    cpt, j = 0, 0\n",
    "    while j < nbd:\n",
    "        som = sum([init[k] for k in range(nbd)])\n",
    "\n",
    "        if som == s:\n",
    "            cpt += 1    # compteur de bonnes solutions\n",
    "        if som == 6*nbd:\n",
    "            break\n",
    "\n",
    "        j = 0\n",
    "        if init[j] < 6:\n",
    "            init[j] += 1\n",
    "        else:\n",
    "            while init[j] == 6:\n",
    "                init[j] = 1\n",
    "                j += 1\n",
    "            init[j] += 1\n",
    "\n",
    "print(\"Il y a {:d} façons de faire {:d} avec {:d} dés.\".format(cpt, s, nbd))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 8\n",
    "\n",
    "n, monge = 5, [1]\n",
    "\n",
    "print('Paquet initial   :', list(range(1, 2*n+1)))\n",
    "\n",
    "for i in range(2, 2*n+1):\n",
    "    if i%2 == 0:  #indice pair\n",
    "        monge.insert(0, i)\n",
    "    else:\n",
    "        monge.append(i)\n",
    "        \n",
    "print('Mélange de Monge :', monge)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 9\n",
    "\n",
    "jours = [\"lundi\", \"mardi\", \"mercredi\", \"jeudi\", \"vendredi\", \"samedi\", \"dimanche\"]\n",
    "mois = [\"janvier\", \"février\", \"mars\", \"avril\", \"mai\", \"juin\", \"juillet\", \"août\",\n",
    "      \"septembre\", \"octobre\", \"novembre\", \"décembre\"]\n",
    "nbjours = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "\n",
    "for jour in jours:\n",
    "    print(f\"Si le 13 janvier est un {jour}, \", end='')\n",
    "    m, j = 0, jours.index(jour)  # initialisation : m = 'janvier' et j = 'lundi'\n",
    "    while j%7 != 4 and m < 12:   # 4 est l'indice du vendredi dans la semaine\n",
    "        j += nbjours[m]\n",
    "        m += 1;\n",
    "    if j%7 == 4:\n",
    "        print(f\"le 13 {mois[m]} sera un vendredi 13\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
