Bertrand Benjamin
809c20cc21
All checks were successful
continuous-integration/drone/push Build is passing
343 lines
10 KiB
Plaintext
343 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bf0de95a",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Les strings\n",
|
|
"\n",
|
|
"Dans ce TP, vous apprendrez les manipulations de bases des chaine de caractères en python (`str`)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e42ebfb4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Str\n",
|
|
"\n",
|
|
"Les `str` en python ont de nombreuses similarités avec les `list` et les `tuple`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "64156203",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mot = \"Salut tout le monde\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6a818ba8",
|
|
"metadata": {},
|
|
"source": [
|
|
"Reprendre le cours sur les listes et les tuples et trouver les similarités avec les `str`. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "640433ba",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f758b002",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "35bbe2e4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ca07c701",
|
|
"metadata": {},
|
|
"source": [
|
|
"Les `str` ont des méthodes spécifiques"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "70da034e",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Salut_tout_le_monde'"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"mot.replace(\" \", \"_\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "952f4936",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'SALUT TOUT LE MONDE'"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"mot.upper()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "45762d2b",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Exercices techniques"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6c221743",
|
|
"metadata": {},
|
|
"source": [
|
|
"1. Ecrire une fonction `renverse` qui prend en argument une chaine de caractère et qui renverse cette dernière."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "24a281d3",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "TypeError",
|
|
"evalue": "renverse() takes 0 positional arguments but 1 was given",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
|
"Input \u001b[0;32mIn [11]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# tests\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[43mrenverse\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mazerty\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mytreza\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
|
|
"\u001b[0;31mTypeError\u001b[0m: renverse() takes 0 positional arguments but 1 was given"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# tests\n",
|
|
"assert renverse(\"azerty\") == \"ytreza\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "7cb00bce",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# votre fonction"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "1691a5f7",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Un palindrome est une chaine de caractère qui est identique peu importe le sens dans lequel on la lit.\n",
|
|
"\n",
|
|
"Écrire une fonction `est_palindrome` qui prend en argument une chaine de caractère et qui renvoie vrai ou faux suivant si elle est un palindrome ou non (on ne tiendra pas compte des espaces)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "b16877c0",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "NameError",
|
|
"evalue": "name 'est_palindrom' is not defined",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
|
"Input \u001b[0;32mIn [9]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# tests\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[43mest_palindrom\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcoucou\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m est_palidrome(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mazertreza\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m est_palindrome(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcaser vite ce palindrome ne mord ni lape cet ivre sac\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
|
|
"\u001b[0;31mNameError\u001b[0m: name 'est_palindrom' is not defined"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# tests\n",
|
|
"assert est_palindrome(\"coucou\") == False\n",
|
|
"assert est_palindrome(\"azertreza\") == True\n",
|
|
"assert est_palindrome('caser vite ce palindrome ne mord ni lape cet ivre sac') == True\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "4f8ca51b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Votre fonction"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a06bc2e4",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Chiffrement\n",
|
|
"\n",
|
|
"Le chiffrement de Césars consiste à décaler l'alphabet dans un message pour le rendre non comprehensible. Le décalage est appelé `clé de chiffrement`.\n",
|
|
"\n",
|
|
"Ainsi si la clé de chiffrement est 2, on aura\n",
|
|
"\n",
|
|
"| message orginnal | message chiffré |\n",
|
|
"| --- | --- |\n",
|
|
"| les | ngu |\n",
|
|
"\n",
|
|
"On supposera que le message original ne contient que des caractères ASCII (entre 32 et 126). Le message chiffré sera lui aussi composé de caractères ASCII entre 32 et 126.\n",
|
|
"\n",
|
|
"1. Écrire une fonction `chiffre_lettre` qui prend en argument la clé de chiffrement et un caractère et qui renvoie le caractère chiffré. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"id": "db6ab5b9",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "NameError",
|
|
"evalue": "name 'chiffre_lettre' is not defined",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
|
"Input \u001b[0;32mIn [13]\u001b[0m, in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# tests\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[43mchiffre_lettre\u001b[49m(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124ml\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mn\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m chiffre_lettre(\u001b[38;5;241m4\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m~\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m chiffre_lettre(\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m10\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m?\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m5\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
|
|
"\u001b[0;31mNameError\u001b[0m: name 'chiffre_lettre' is not defined"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# tests\n",
|
|
"assert chiffre_lettre(2, 'l') == 'n'\n",
|
|
"assert chiffre_lettre(4, '~') == '#'\n",
|
|
"assert chiffre_lettre(-10, '?') == '5'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"id": "b5b0a910",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Fonction"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "765d8791",
|
|
"metadata": {},
|
|
"source": [
|
|
"2. Écrire une fonction `chiffre_message` qui prend en argument la clé de chiffrement et le message et qui renvoie tout le message chiffré."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "93ab941b",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ca7c256c",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "43e00026",
|
|
"metadata": {},
|
|
"source": [
|
|
"3. Écrire une fonction `dechiffre_message` qui prend en argument la clé de chiffrement et un message chiffré et qui renvoie le message déchiffré."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "395c7b40",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "534209fd",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "271a2087",
|
|
"metadata": {},
|
|
"source": [
|
|
"4. Statistiquement, le caractère le plus présent dans un message et l'espace. Servez vous de cette connaissance pour écrire une fonction qui sera capable de trouver "
|
|
]
|
|
}
|
|
],
|
|
"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.10.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|