{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recherche_binaire(L, x):\n",
    "    '''\n",
    "    Entree: nombre x, liste L de nombres triee\n",
    "    Sortie: i t.q. L[i]=x s'il existe, None sinon\n",
    "    '''\n",
    "    n = len(L)\n",
    "    bas = 0\n",
    "    haut = n-1\n",
    "    \n",
    "    while haut >= bas:\n",
    "        milieu = (bas + haut)//2\n",
    "        if L[milieu] == x:\n",
    "            return milieu        \n",
    "        elif L[milieu] > x:\n",
    "            haut = milieu - 1\n",
    "        else:\n",
    "            bas = milieu + 1\n",
    "            \n",
    "\n",
    "def fusion(L, bas, milieu, haut):\n",
    "    '''Entree: liste L t.q. L[bas:milieu+1] et \n",
    "            L[milieu+1:haut+1] sont triees\n",
    "    Trie L[bas:haut+1]'''\n",
    "    L1 = L[bas:milieu+1]\n",
    "    L2 = L[milieu+1:haut+1]\n",
    "    L1.append(float('inf'))\n",
    "    L2.append(float('inf'))\n",
    "    L1_index = 0\n",
    "    L2_index = 0  \n",
    "    for i in range(bas, haut+1):\n",
    "        if L1[L1_index] <= L2[L2_index]:\n",
    "            L[i] = L1[L1_index]\n",
    "            L1_index += 1\n",
    "        else:\n",
    "            L[i] = L2[L2_index]\n",
    "            L2_index += 1\n",
    "            \n",
    "            \n",
    "def tri_par_fusion(L, bas, haut):\n",
    "    '''\n",
    "    Entree: liste L, indices bas et haut\n",
    "    Trie la tranche de L entre bas et haut\n",
    "    '''\n",
    "    if haut - bas > 0:      \n",
    "        milieu = (bas + haut)//2       \n",
    "        tri_par_fusion(L, bas, milieu)\n",
    "        tri_par_fusion(L, milieu+1, haut)\n",
    "        fusion(L, bas, milieu, haut)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def somme_deux_naif(x, L):\n",
    "    '''\n",
    "    Entree: liste L de nombres distincts, nombre x\n",
    "    Sortie: L[i], L[j] distincts tq L[i] + L[j] = x\n",
    "            None si aucuns tels L[i], L[j] n'existent\n",
    "    '''\n",
    "    n = len(L)\n",
    "    for i in range(n):\n",
    "        for j in range(i+1, n):\n",
    "            if L[i] + L[j] == x:\n",
    "                return L[i], L[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def somme_deux(x, L):\n",
    "    '''\n",
    "    Entree: liste L de nombres distincts, nombre x\n",
    "    Sortie: L[i], L[j] distincts tq L[i] + L[j] = x\n",
    "            None si aucuns tels L[i], L[j] n'existent\n",
    "    '''\n",
    "    n = len(L)\n",
    "    tri_par_fusion(L, 0, n-1)\n",
    "    \n",
    "    for i in range(n):\n",
    "        if x - L[i] == L[i]:\n",
    "            return None #pour ne pas retourner (L[i],L[i])     \n",
    "        j = recherche_binaire(L, x - L[i])\n",
    "        if j != None:\n",
    "            return L[i], L[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def somme_deux_lineaire(x, L):\n",
    "    '''\n",
    "    Entree: liste L triee de nombres distincts, nombre x\n",
    "    Sortie: L[i], L[j] distincts tq L[i] + L[j] = x\n",
    "            None si aucuns tels L[i], L[j] n'existent\n",
    "    '''\n",
    "    i = 0\n",
    "    j = len(L) - 1\n",
    "    \n",
    "    while i < j:\n",
    "        if L[i] + L[j] == x:\n",
    "            return L[i], L[j]\n",
    "        \n",
    "        if L[i] + L[j] < x:\n",
    "            i += 1\n",
    "        else:\n",
    "            j -= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1)\n",
      "(-7, 8)\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "L = [0, 1, -2, -1, 5, -7, 9, 8]\n",
    "print(somme_deux_naif(1, L))\n",
    "print(somme_deux(1,L))\n",
    "L = [0, 1, -2, -1, 5, -7, 9, 8]\n",
    "\n",
    "print(somme_deux_lineaire(1,L))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
