{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercices du livre - Chapitre 6 - 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": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 1\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from math import gcd\n",
    "\n",
    "# Programme principal ==========================================================\n",
    "n = int(input(\"Numérateur : \"))\n",
    "d = int(input(\"Dénominateur : \"))\n",
    "while d == 0:\n",
    "    d = int(input(\"Dénominateur non nul S.V.P : \"))\n",
    "\n",
    "f = gcd(n, d)\n",
    "\n",
    "print(f\"\\nFraction réduite : {n//f} / {d//f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 2 (module)\n",
    "\n",
    "# import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from turtle import forward, left, width, done\n",
    "\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def polygone_regulier(ncotes, longueur):\n",
    "    angle = 360/ncotes\n",
    "    for i in range(ncotes):\n",
    "        forward(longueur)\n",
    "        left(angle)\n",
    "\n",
    "\n",
    "# Auto-test ===================================================================\n",
    "if __name__=='__main__':\n",
    "    width(3)\n",
    "\n",
    "    polygone_regulier(3, 300)\n",
    "    polygone_regulier(4, 80)\n",
    "    polygone_regulier(8, 100)\n",
    "    polygone_regulier(100, 5)\n",
    "\n",
    "    done()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 2 (programme principal)\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from turtle import left, width, done\n",
    "from turtle_m import polygone_regulier\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def polygones(npoly, ncotes, longueur):\n",
    "    rot = 360 / npoly\n",
    "    for i in range(npoly):\n",
    "        polygone_regulier(ncotes, longueur)\n",
    "        left(rot)\n",
    "\n",
    "# programme principal =========================================================\n",
    "width(3)\n",
    "\n",
    "polygones(10, 3, 150)\n",
    "\n",
    "done()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercice 3\n",
    "\n",
    "import string\n",
    "\n",
    "def est_pangramme(s, alphabet=string.ascii_lowercase):\n",
    "    alphaset = set(alphabet)\n",
    "    return alphaset <= set(s.lower())\n",
    "\n",
    "if __name__ == \"__main__\"        :\n",
    "    s1 = \"Portez ce vieux whisky au juge blond qui fume\"\n",
    "    s2 = \"Le vif renard brun saute par-dessus le chien paresseux\"\n",
    "\n",
    "    print(est_pangramme(s1))\n",
    "    print(est_pangramme(s2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercice 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 4 (module)\n",
    "\n",
    "# Définition de fonction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "def somDiv(n):\n",
    "    \"\"\"Retourne la somme des diviseurs propres de <n>.\"\"\"\n",
    "    som_div = 1\n",
    "    for div in range(2, (n//2)+1):\n",
    "        if n % div == 0:\n",
    "            som_div += div\n",
    "    return som_div\n",
    "\n",
    "\n",
    "def estParfait(n):\n",
    "    \"\"\"Retourne True si <n> est parfait, False sinon.\"\"\"\n",
    "    return somDiv(n) == n\n",
    "\n",
    "\n",
    "def estPremier(n):\n",
    "    \"\"\"Retourne True si <n> est premier, False sinon.\"\"\"\n",
    "    return somDiv(n) == 1\n",
    "\n",
    "\n",
    "def estChanceux(n):\n",
    "    \"\"\"Retourne True si <n> est chanceux, False sinon.\"\"\"\n",
    "    est_chanceux = True\n",
    "    for i in range(0, n-1):\n",
    "        est_chanceux = est_chanceux and estPremier(n + i + i*i)\n",
    "    return est_chanceux\n",
    "\n",
    "# Auto-test ===================================================================\n",
    "if __name__=='__main__':\n",
    "    from math import isclose\n",
    "\n",
    "    print(isclose(somDiv(12), 16))\n",
    "    print(isclose(estParfait(6), True))\n",
    "    print(isclose(estPremier(31), True))\n",
    "    print(isclose(estChanceux(11), True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Exercice 4 (programme principal)\n",
    "\n",
    "# Import ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "from parfait_chanceux_m import estParfait, estChanceux\n",
    "\n",
    "# programme principal =========================================================\n",
    "parfait, chanceux = [], []\n",
    "\n",
    "for n in range(2, 1001):\n",
    "    if estParfait(n):\n",
    "        parfait.append(n)\n",
    "    if estChanceux(n):\n",
    "        chanceux.append(n)\n",
    "        \n",
    "print(\"\\nIl y a {} nombres parfaits dans [2, 1000] : {}\".format(len(parfait), parfait))\n",
    "print(\"\\nIl y a {} nombres chanceux dans [2, 1000] : {}\".format(len(chanceux), chanceux))"
   ]
  }
 ],
 "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
}
