{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 3 - 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",
    "n = int(input(\"Entrez un entier positif : \"))\n",
    "while n < 0:\n",
    "    n = int(input(\"Entrez un entier positif, S.V.P : \"))\n",
    "\n",
    "print(f\"\\nSomme des {n} premiers entiers : {n * (n+1) // 2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 2\n",
    "\n",
    "\n",
    "from math import log10\n",
    "\n",
    "# Saisies filtrées\n",
    "a = int(input(\"Entrez un entier strictement positif : \"))\n",
    "while a <= 0:\n",
    "    a = int(input(\"Entrez un entier strictement positif, S.V.P : \"))\n",
    "b = int(input(\"Entrez un entier non nul : \"))\n",
    "while b == 0:\n",
    "    b = int(input(\"Entrez un entier non nul, S.V.P : \"))\n",
    "\n",
    "print(f\"\\n{a} + {b} = {a + b}\")\n",
    "print(f\"{a} - {b} = {a - b}\")\n",
    "print(f\"{a} * {b} = {a * b}\")\n",
    "print(f\"{a} / {b} = {a / b:.4g}\")\n",
    "print(f\"{a} % {b} = {a % b}\")\n",
    "\n",
    "print(f\"\\nLogarithme décimal de {a} : {log10(a):.4g}\")\n",
    "print(f\"{a} exposant {b} = {a ** b:.4g}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 3\n",
    "\n",
    "CAPA_CALO_EAU = 4.186  # capacité calorifique de l'eau\n",
    "C = 3e8                # célérité  de la lumière (en m/s)\n",
    "\n",
    "# Saisies filtrées\n",
    "volume = float(input(\"Volume d'eau (ml) : \"))\n",
    "while volume < 0:\n",
    "    volume = float(input(\"Volume d'eau (ml), positif S.V.P : \"))\n",
    "d_temp = float(input(\"Accroissement de température de la pièce (°C) : \"))\n",
    "while d_temp < 0:\n",
    "    d_temp = float(input(\"Accroissement de température de la pièce (°C), positif S.V.P : \"))\n",
    "\n",
    "delta_E = volume * d_temp * CAPA_CALO_EAU\n",
    "\n",
    "print(f\"\\nCet échauffement requiert une énergie de : {delta_E:.3g} J.\")\n",
    "\n",
    "delta_m = delta_E / C**2  # accroissement de la masse\n",
    "print(f\"Accroissement de la masse : {delta_m:.2g} kg, soit : {delta_m  * 1e12:.2f} nanogrammes\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 4\n",
    "\n",
    "while n < 1:\n",
    "    n = int(input(\"Entrez un entier STRICTEMENT POSITIF, s.v.p. : \"))\n",
    "\n",
    "i = 2       # plus petit diviseur possible de n\n",
    "cpt = 0     # initialise le compteur de divisions\n",
    "p = n/2     # calculé une fois dans la boucle\n",
    "\n",
    "print(\"Diviseurs propres sans répétition de \", n, \":\", end=' ')\n",
    "while i <= p:\n",
    "    if n%i == 0:\n",
    "        cpt += 1\n",
    "        print(i, end=' ')\n",
    "    i += 1\n",
    "\n",
    "if not cpt:\n",
    "    print(\"aucun ! C'est un nombre premier.\")\n",
    "else:\n",
    "    print(\"(soit\", cpt, \"diviseurs propres)\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 5\n",
    "\n",
    "H = 'abcdefgh'  # repère horizontal\n",
    "\n",
    "# Saisies filtrées de la position\n",
    "lettre = input(\"Position horizontale ['a'.. 'h'] : \").lower()\n",
    "while lettre not in H:\n",
    "    lettre = input(\"Position horizontale ['a'.. 'h'], S.V.P : \").lower()\n",
    "nombre = int(input(\"Position verticale [1.. 8] : \"))\n",
    "while not (1 <= nombre <= 8):\n",
    "    nombre = int(input(\"Position verticale [1.. 8], S.V.P : \"))\n",
    "\n",
    "# On postule que noir est vrai et blanc faux\n",
    "coul_lettre = True if H.index(lettre)%2 == 0 else False\n",
    "coul = coul_lettre if nombre%2 == 1 else not(coul_lettre)\n",
    "\n",
    "print(f\"\\nLa position '{lettre}{nombre}' est une case {'noire' if coul else 'blanche'}.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 6\n",
    "\n",
    "annee = int(input(\"Entrez une année : \"))\n",
    "while annee <= 0:\n",
    "    annee = int(input(\"Entrez une année après J.C S.V.P : \"))\n",
    "\n",
    "est_bissextile = ((annee % 4 == 0) and (annee % 100 != 0)) or (annee % 400 == 0)\n",
    "msg = \"est\" if est_bissextile else \"n'est pas\"\n",
    "\n",
    "print(f\"\\nL'année {annee} {msg} bissextile.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 7\n",
    "\n",
    "line = input(\"\\nEntrez 8 bits (ligne vide pour terminer) : \")\n",
    "while line:\n",
    "    while (line.count('0') + line.count('1') != 8) or len(line) != 8:\n",
    "        line = input(\"Entrez 8 bits, S.V.P : \")\n",
    "    print(f\"\\tLe bit de parité est {int(line.count('1') == 0)}\")\n",
    "    line = input(\"Entrez 8 bits (ligne vide pour terminer) : \")\n",
    "\n",
    "print(\"\\nTerminé !\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 8\n",
    "\n",
    "n = int(input('Entrez un entier [1 .. 4000[ : '))\n",
    "while not(n >= 1 and n < 4000):\n",
    "    n = int(input('Entrez un entier [1 .. 4000[, s.v.p. : '))\n",
    "\n",
    "s = \"\"  # Chaîne résultante\n",
    "\n",
    "while n >= 1000:\n",
    "    s += \"M\"\n",
    "    n -= 1000\n",
    "\n",
    "if n >= 900:\n",
    "    s += \"CM\"\n",
    "    n -= 900\n",
    "\n",
    "if n >= 500:\n",
    "    s += \"D\"\n",
    "    n -= 500\n",
    "\n",
    "if n >= 400:\n",
    "    s += \"CD\"\n",
    "    n -= 400\n",
    "\n",
    "while n >= 100:\n",
    "    s += \"C\"\n",
    "    n -= 100\n",
    "\n",
    "if n >= 90:\n",
    "    s += \"XC\"\n",
    "    n -= 90\n",
    "\n",
    "if n >= 50:\n",
    "    s += \"L\"\n",
    "    n -= 50\n",
    "\n",
    "if n >= 40:\n",
    "    s += \"XL\"\n",
    "    n -= 40\n",
    "\n",
    "while n >= 10:\n",
    "    s += \"X\"\n",
    "    n -= 10\n",
    "\n",
    "if n >= 9:\n",
    "    s += \"IX\"\n",
    "    n -= 9\n",
    "\n",
    "if n >= 5:\n",
    "    s += \"V\"\n",
    "    n -= 5\n",
    "\n",
    "if n >= 4:\n",
    "    s += \"IV\"\n",
    "    n -= 4\n",
    "\n",
    "while n >= 1:\n",
    "    s += \"I\"\n",
    "    n -= 1\n",
    "\n",
    "print(\"En romain :\", s)"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
