{ "cells": [ { "cell_type": "markdown", "id": "bce10e35", "metadata": {}, "source": [ "# Exercices Python\n", "\n", "## Introduction\n", "\n", "### Intégration: méthode des rectangles" ] }, { "cell_type": "code", "execution_count": 1, "id": "983031a0", "metadata": {}, "outputs": [], "source": [ "def integ_rectangles(f, a, b, n=100):\n", " \"\"\"\n", " Retourne une évaluation de l'intégrale de la fonction `f` entre `a` et `b` \n", " par la méthode des rectangles, avec `n` pas.\n", " \"\"\"\n", "\n", " return 0" ] }, { "cell_type": "markdown", "id": "4a8c0c44", "metadata": {}, "source": [ "### Fizz-Buzz" ] }, { "cell_type": "code", "execution_count": 2, "id": "adc29f43", "metadata": {}, "outputs": [], "source": [ "def fizzbuzz(n):\n", " \"\"\"\n", " Retourne une chaîne de caractères à imprimer.\n", " \"\"\"\n", " \n", " return ''" ] }, { "cell_type": "markdown", "id": "69eb4f19", "metadata": {}, "source": [ "### PGCD: algorithme d'Euclide" ] }, { "cell_type": "code", "execution_count": 3, "id": "66706384", "metadata": {}, "outputs": [], "source": [ "def pgcd(a, b):\n", " \"\"\"\n", " Calcul du PGCD de deux entiers 0 < b < a.\n", " \"\"\"\n", " \n", " return 0" ] }, { "cell_type": "markdown", "id": "22f5ba2e", "metadata": {}, "source": [ "## Manipulation de listes\n", "\n", "### Crible d'Ératosthène" ] }, { "cell_type": "code", "execution_count": 4, "id": "fc46b886", "metadata": {}, "outputs": [], "source": [ "def erathostene(n):\n", " \"\"\"\n", " Retourne la liste des entiers premiers ≤ n.\n", " \"\"\"\n", " \n", " return []" ] }, { "cell_type": "markdown", "id": "8fb44f16", "metadata": {}, "source": [ "### Carré magique" ] }, { "cell_type": "code", "execution_count": 5, "id": "bedb8ea1", "metadata": {}, "outputs": [], "source": [ "def carremagique(n):\n", " \"\"\"\n", " Retourne le carré magique d'ordre n (impair).\n", " \"\"\"\n", " \n", " return [[]]" ] }, { "cell_type": "markdown", "id": "556fdf19", "metadata": {}, "source": [ "## Programmation\n", "\n", "### Suite de Syracuse" ] }, { "cell_type": "code", "execution_count": 6, "id": "454b1bac", "metadata": {}, "outputs": [], "source": [ "def suite_syracuse(n):\n", " \"\"\"\n", " Retourne la suite de Syracuse d'ordre n.\n", " \"\"\"\n", " \n", " return []\n", " \n", "def temps_syracuse(n, altitude=False):\n", " \"\"\"\n", " Retourne le temps de vol (total ou en altitude) de la suite de Syracuse d'ordre n.\n", " \"\"\"\n", " \n", " return 0" ] }, { "cell_type": "markdown", "id": "68c62be1", "metadata": {}, "source": [ "## Tests de correction\n", "\n", "**NE PAS MODIFIER/EFFACER!**" ] }, { "cell_type": "code", "execution_count": 7, "id": "ab6aac9c", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "test_01_integ_rectangles (__main__.TestNotebook) ... FAIL\n", "test_02_fizzbuzz (__main__.TestNotebook) ... FAIL\n", "test_03_pgcd (__main__.TestNotebook) ... FAIL\n", "test_04_erathostene (__main__.TestNotebook) ... FAIL\n", "test_05_carremagique (__main__.TestNotebook) ... FAIL\n", "test_06_syracuse (__main__.TestNotebook) ... FAIL\n", "\n", "======================================================================\n", "FAIL: test_01_integ_rectangles (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 6, in test_01_integ_rectangles\n", " self.assertAlmostEqual(integ_rectangles(lambda x: x**2, 0, 1, 100), 1/3, places=4)\n", "AssertionError: 0 != 0.3333333333333333 within 4 places (0.3333333333333333 difference)\n", "\n", "======================================================================\n", "FAIL: test_02_fizzbuzz (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 9, in test_02_fizzbuzz\n", " self.assertEqual(fizzbuzz(16),\n", "AssertionError: '' != '1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16'\n", "+ 1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16\n", "\n", "======================================================================\n", "FAIL: test_03_pgcd (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 13, in test_03_pgcd\n", " self.assertEqual(pgcd(756, 306), 18)\n", "AssertionError: 0 != 18\n", "\n", "======================================================================\n", "FAIL: test_04_erathostene (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 18, in test_04_erathostene\n", " self.assertEqual(erathostene(41),\n", "AssertionError: Lists differ: [] != [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]\n", "\n", "Second list contains 13 additional elements.\n", "First extra element 0:\n", "2\n", "\n", "- []\n", "+ [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]\n", "\n", "======================================================================\n", "FAIL: test_05_carremagique (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 27, in test_05_carremagique\n", " self.assertEqual(carremagique(5), carre)\n", "AssertionError: Lists differ: [[]] != [[11, 18, 25, 2, 9], [10, 12, 19, 21, 3], [[53 chars] 15]]\n", "\n", "First differing element 0:\n", "[]\n", "[11, 18, 25, 2, 9]\n", "\n", "Second list contains 4 additional elements.\n", "First extra element 1:\n", "[10, 12, 19, 21, 3]\n", "\n", "- [[]]\n", "+ [[11, 18, 25, 2, 9],\n", "+ [10, 12, 19, 21, 3],\n", "+ [4, 6, 13, 20, 22],\n", "+ [23, 5, 7, 14, 16],\n", "+ [17, 24, 1, 8, 15]]\n", "\n", "======================================================================\n", "FAIL: test_06_syracuse (__main__.TestNotebook)\n", "----------------------------------------------------------------------\n", "Traceback (most recent call last):\n", " File \"/tmp/ipykernel_2267707/903517605.py\", line 32, in test_06_syracuse\n", " self.assertEqual(suite_syracuse(15),\n", "AssertionError: Lists differ: [] != [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]\n", "\n", "Second list contains 18 additional elements.\n", "First extra element 0:\n", "15\n", "\n", "- []\n", "+ [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1]\n", "\n", "----------------------------------------------------------------------\n", "Ran 6 tests in 0.006s\n", "\n", "FAILED (failures=6)\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import unittest\n", "\n", "class TestNotebook(unittest.TestCase):\n", " \n", " def test_01_integ_rectangles(self):\n", " self.assertAlmostEqual(integ_rectangles(lambda x: x**2, 0, 1, 100), 1/3, places=4)\n", " \n", " def test_02_fizzbuzz(self):\n", " self.assertEqual(fizzbuzz(16), \n", " \"1 2 Fizz! 4 Buzz! Fizz! 7 8 Fizz! Buzz! 11 Fizz! 13 14 Fizz Buzz! 16\")\n", "\n", " def test_03_pgcd(self):\n", " self.assertEqual(pgcd(756, 306), 18)\n", " with self.assertRaises(AssertionError):\n", " pgcd(306, 756)\n", " \n", " def test_04_erathostene(self):\n", " self.assertEqual(erathostene(41), \n", " [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41])\n", " \n", " def test_05_carremagique(self):\n", " carre = [[11, 18, 25, 2, 9],\n", " [10, 12, 19, 21, 3],\n", " [4, 6, 13, 20, 22],\n", " [23, 5, 7, 14, 16],\n", " [17, 24, 1, 8, 15]]\n", " self.assertEqual(carremagique(5), carre)\n", " with self.assertRaises(AssertionError):\n", " carremagique(6)\n", "\n", " def test_06_syracuse(self):\n", " self.assertEqual(suite_syracuse(15),\n", " [15, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1])\n", " self.assertEqual(temps_syracuse(15), 17)\n", " self.assertEqual(temps_syracuse(15, altitude=True), 11)\n", " \n", "unittest.main(argv=[''], verbosity=2, exit=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "556857f2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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.8.10" }, "nbsphinx": { "orphan": true }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 5 }