2019-2020/Formations/NSI/Bloc1/TD2.ipynb

698 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercice 6"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def cherche(tableau, elem):\n",
" \"\"\"Y a-t-il elem dans le tableau\"\"\"\n",
" for e in tableau:\n",
" if elem == e:\n",
" return 1\n",
" return 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cherche(list(range(50)), 5)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"cherche(list(range(50)), 100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercice 7"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def dico_rec(tableau, elem):\n",
" size = len(tableau)\n",
" if size == 0:\n",
" return 0\n",
" \n",
" middle = tableau[int(size/2)]\n",
" \n",
" if elem == middle:\n",
" return 1\n",
" elif elem > middle:\n",
" return dico_rec(tableau[int(size/2)+1:], elem)\n",
" elif elem < middle:\n",
" return dico_rec(tableau[:int(size/2)], elem)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dico_rec(list(range(11)), 1)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dico_rec(list(range(10)), 6)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dico_rec(list(range(11)), 12)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dico_rec(list(range(10)), 14)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Triée?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def is_sorted(tab):\n",
" \"\"\" Trie croissant? \"\"\"\n",
" for i in range(len(tab)-1):\n",
" if tab[i] > tab[i+1]:\n",
" return 0\n",
" return 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_sorted(list(range(10)))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_sorted([3, 4, 1, 2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tri insertion"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def insert(tab, elem, head=[]):\n",
" \"\"\" Insert elem au bon endroit dans tab \n",
" \n",
" param tab: \n",
" param elem: élément à inserer\n",
" param head: élément plus petits que elem\n",
" \"\"\"\n",
" if tab == []:\n",
" return head+[elem]\n",
" first = tab[0]\n",
" if first >= elem:\n",
" return head + [elem] + tab\n",
" else:\n",
" return insert(tab[1:], elem, head+[first])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 4, 5, 6]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"insert([1, 4, 5, 6], 2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 5, 6]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"insert([1, 4, 5, 6], 0)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 5, 6, 10]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"insert([1, 4, 5, 6], 10)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def tri_insertion(tab, out=[]):\n",
" \"\"\" Trie par insertion\n",
" \n",
" param tab\n",
" \"\"\"\n",
" if tab == []:\n",
" return out\n",
" elem = tab[0]\n",
" new_out = insert(out, elem)\n",
" return tri_insertion(tab[1:], new_out)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 6]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_insertion([3, 4, 2, 6, 1])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_insertion([])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 3, 5, 6]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_insertion([3, 3, 5, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tri selection"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"def split_min(tab, minval=None, head = [], mid_head=[]):\n",
" \"\"\" coupecoupe\n",
" \n",
" return: head, min, tail\n",
" \"\"\"\n",
" if tab == []:\n",
" return head, minval, mid_head\n",
" if minval==None:\n",
" return split_min(tab[1:], tab[0], mid_head)\n",
" \n",
" first = tab[0]\n",
" if first > minval:\n",
" return split_min(tab[1:], minval, head, mid_head+[first])\n",
" \n",
" return split_min(tab[1:], first , head + [minval] + mid_head, [])"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([3, 4], 1, [6, 5])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"split_min([3, 4, 1, 6, 5])"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([6], 5, [])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"split_min([6, 5])"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([], 5, [6])"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"split_min([5, 6])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([], 4, [])"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"split_min([4])"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"def tri_selection(tab):\n",
" \"\"\" Pas sur que ce soit vraiment ça qu'on demande \"\"\"\n",
" if len(tab)==0:\n",
" return []\n",
" if len(tab)==1:\n",
" return tab\n",
" first = tab[0]\n",
" head, minval, tail = split_min(tab[1:])\n",
" if first > minval:\n",
" return [minval]+ tri_selection(head + [first] + tail)\n",
" return [first] + tri_selection(tab[1:])"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 6]"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_selection([3, 4, 2, 6, 1])"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_selection([])"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 3, 5, 6]"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_selection([3, 3, 5, 6])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tri fusion"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
"def fusion(head, tail):\n",
" \"\"\" Fusion les 2 tableaux\n",
" \n",
" param head: liste d'entiers triée\n",
" param tail: liste d'entiers triée\n",
" return: fusion des deux listes ordonnées\n",
" \"\"\"\n",
" try:\n",
" last_head = head[-1]\n",
" except IndexError:\n",
" return tail\n",
" try:\n",
" last_tail= tail[-1]\n",
" except IndexError:\n",
" return head\n",
" \n",
" if last_head > last_tail:\n",
" return fusion(head[:-1], tail) + [last_head]\n",
" return fusion(head, tail[:-1]) + [last_tail]"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 3, 4]"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fusion([3, 4], [2])"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"def tri_fusion(tab):\n",
" \"\"\"\"\"\"\n",
" if len(tab)<2:\n",
" return tab\n",
" middle = int(len(tab)/2)\n",
" head = tri_fusion(tab[middle:])\n",
" tail = tri_fusion(tab[:middle])\n",
" return fusion(head, tail)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 5, 5, 19]"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tri_fusion([3, 5, 5, 19, 1, 2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tri "
]
}
],
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}