{"id":253,"date":"2021-11-22T22:23:51","date_gmt":"2021-11-22T21:23:51","guid":{"rendered":"http:\/\/yb-isn.fr\/2021\/nsi\/?p=253"},"modified":"2021-12-06T22:45:18","modified_gmt":"2021-12-06T21:45:18","slug":"resolution-de-probleme","status":"publish","type":"post","link":"http:\/\/yb-isn.fr\/2021\/nsi\/?p=253","title":{"rendered":"R\u00e9solution de probl\u00e8me"},"content":{"rendered":"<p><!--more--><\/p>\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\">1) <strong>\u00c9nonce et formalisation<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1.png\" alt=\"\" class=\"wp-image-254\" width=\"768\" height=\"432\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1.png 1024w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1-300x169.png 300w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1-768x432.png 768w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1-350x197.png 350w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/plssc-1024x576-1-860x484.png 860w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image.png\" alt=\"\" class=\"wp-image-267\" width=\"617\" height=\"201\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image.png 823w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-300x98.png 300w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-768x250.png 768w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-1.png\" alt=\"\" class=\"wp-image-269\" width=\"405\" height=\"161\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-1.png 810w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-1-300x119.png 300w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-1-768x304.png 768w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><\/figure>\n\n\n\n<p>La suite d\u2019immeubles sera mod\u00e9lis\u00e9e par un tableau d\u2019entiers strictement positifs repr\u00e9sentants la hauteur d\u2019un immeuble ou le nombre d&rsquo;\u00e9tages.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#repr\u00e9sentation de la suite d'immeubles\nsuiteImmeubles=[5,1,8,2,7,6,10,3,9,11]<\/pre>\n\n\n\n<p>En d\u00e9truisant les immeubles en gris nous avons une solution au probl\u00e8me pos\u00e9 pour cet exemple. Il faut donc extraire de la suite d&rsquo;immeubles <strong>une<\/strong> plus grande sous suite croissante.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/data-plssc\/sous-suite-immeubles3.png\" alt=\"\" width=\"161\" height=\"128\"\/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#repr\u00e9sentation de la sous suite d'immeubles \u00e0 conserver\nsousSuiteImmeubles=[1,2,7,9,11]<\/pre>\n\n\n<p><b>Nous allons pas \u00e0 pas vers une r\u00e9solution na\u00efve (force brute)&nbsp; du probl\u00e8me<\/b><\/p>\n<ul>\n<li>rechercher le nombre de sous suites possibles .<\/li>\n<li>\u00c9num\u00e9rer toutes les sous suites<\/li>\n<li>Trouver une plus grande parmi celles qui sont croissantes<\/li>\n<li>Visualiser le r\u00e9sultat obtenu en option<\/li>\n<\/ul>\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\">2) <strong>une suite d&rsquo;entier est elle croissante<\/strong> ?<\/p>\n\n\n\n<p>Nous allons coder une fonction python : <strong>croissante()<\/strong><\/p>\n\n\n\n<p>Une liste de type list en entr\u00e9e<\/p>\n\n\n\n<p>Une valeur bool\u00e9enne en sortie<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def croissante(suite:list)->bool:\n    \"\"\"renvoi True si la liste est croissante False sinon\"\"\"\n    for i in range(len(suite)-1):\n        if suite[i]>=suite[i+1]:\n            return False\n    return True<\/pre>\n\n\n\n<p>La fonction est document\u00e9e avec les arguments de l&rsquo;entr\u00e9e et de la sortie<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-2.png\" alt=\"\" class=\"wp-image-280\" width=\"140\" height=\"19\"\/><\/figure>\n\n\n\n<p>Un docsstring a \u00e9t\u00e9 ajout\u00e9 pour avoir une aide sur la fonction<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-3.png\" alt=\"\" class=\"wp-image-281\" width=\"382\" height=\"18\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-3.png 764w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-3-300x14.png 300w\" sizes=\"(max-width: 382px) 100vw, 382px\" \/><\/figure>\n\n\n\n<p>Pour avoir une aide sur la fonction il suffit d&rsquo;utiliser la fonction help()<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-4.png\" alt=\"\" class=\"wp-image-282\" width=\"428\" height=\"104\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-4.png 570w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-4-300x73.png 300w\" sizes=\"(max-width: 428px) 100vw, 428px\" \/><figcaption>quelques tests sur la fonction:<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-5.png\" alt=\"\" class=\"wp-image-285\" width=\"292\" height=\"125\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-5.png 389w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-5-300x129.png 300w\" sizes=\"(max-width: 292px) 100vw, 292px\" \/><\/figure>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\">3)<strong>\u00c9num\u00e9rons toutes les sous suites possibles d&rsquo;une suite<\/strong><\/p>\n\n\n\n<p>2 choix possibles pour chacun des immeubles de la suite (conserver ou d\u00e9molir)<\/p>\n\n\n\n<p>donc&nbsp;2<sup>n<\/sup> &nbsp;choix possibles.<\/p>\n\n\n\n<p>Un nombre binaire k de n bits (avec un bit \u00e0 1 pour conserver et \u00e0 0 pour d\u00e9molir) peut repr\u00e9senter une sous suite.<\/p>\n\n\n\n<p>Exemple avec la suite [1,2,6,5] on souhaite coder la sous suite [1,2]<\/p>\n\n\n\n<p>On \u00e9crira 1100   pour conserver 1 et 2 et d\u00e9molir 6 et 5.<\/p>\n\n\n\n<p>La suite est constitu\u00e9e de 4 immeubles on donc 2<sup>4<\/sup> sous suites au total soit 16<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/Capture-decran-2021-11-27-175342.png\" alt=\"\" class=\"wp-image-290\" width=\"242\" height=\"310\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/Capture-decran-2021-11-27-175342.png 323w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/Capture-decran-2021-11-27-175342-235x300.png 235w\" sizes=\"(max-width: 242px) 100vw, 242px\" \/><\/figure>\n\n\n\n<p>La sous suite obtenue apr\u00e8s d\u00e9molition est repr\u00e9sent\u00e9e par [1,2]<\/p>\n\n\n\n<p>Nous allons coder une fonction sous_suite()<\/p>\n\n\n\n<p>En entr\u00e9e 2 arguments une suite (liste x)  et un entier (k de type int)  repr\u00e9sentant une sous suite<\/p>\n\n\n\n<p>En sortie une variable b de type list repr\u00e9sentant la sous suite<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def sous_suite(x:list,k:int)->list:\n    \"\"\" n est la longueur de la suite x \n        l est le nombre de sous suites\n        \"\"\"      \n    n=len(x)\n    l=2**n\n    a=list(np.binary_repr(k, width=n))\n    b=[]\n    for j in range(n):\n        if a[j]=='1':\n            b.append(int(x[j]))\n    return b<\/pre>\n\n\n\n<p>Un test de la fonction<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-7.png\" alt=\"\" class=\"wp-image-300\" width=\"227\" height=\"53\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-7.png 302w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-7-300x70.png 300w\" sizes=\"(max-width: 227px) 100vw, 227px\" \/><\/figure>\n\n\n\n<p>\u00e0 vous de faire d&rsquo;autres tests et de mieux documenter la fonction pour la comprendre<\/p>\n\n\n\n<p>On peut maintenant exploiter notre fonction pour lister toutes les sous suites possibles<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">suiteImmeubles=[1,2,6,3]\n\nfor l in range(2**(len(suiteImmeubles))):\n    b=sous_suite(suiteImmeubles,l)\n    print(l,\";\",b)<\/pre>\n\n\n\n<p>Un test:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-9.png\" alt=\"\" class=\"wp-image-310\" width=\"148\" height=\"264\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-9.png 197w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-9-168x300.png 168w\" sizes=\"(max-width: 148px) 100vw, 148px\" \/><\/figure>\n\n\n\n<p class=\"has-cyan-bluish-gray-background-color has-background\">4)Trouver une plus grande sous suite parmi celles qui sont croissantes<\/p>\n\n\n\n<p>\u00e0 vous de jouer pour finaliser la r\u00e9solution <\/p>\n\n\n\n<p>corrig\u00e9<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def une_plus_grande_sous_suite(x):\n    n=len(x)\n    l=2**n\n    longest=0\n    for k in range (l):  \n        b=sous_suite(x,k)\n        if croissante(b):\n            if len(b)>longest:\n                k_int=k\n                longest=len(b)\n    solution=sous_suite(x,k_int)\n    return solution<\/pre>\n\n\n\n<p>On teste la fonction sur des listes al\u00e9atoires<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import random\nimport time\n\ndebut=time.time()\n\nT=list(range(10))\nrandom.shuffle(T)\nprint(T)\n\nt=une_plus_grande_sous_suite(T)\nduree=-debut+time.time()\nprint(t)\n\nprint(duree,\"s\")<\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"307\" height=\"76\" src=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-14.png\" alt=\"\" class=\"wp-image-342\" srcset=\"http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-14.png 307w, http:\/\/yb-isn.fr\/2021\/nsi\/wp-content\/uploads\/2021\/11\/image-14-300x74.png 300w\" sizes=\"(max-width: 307px) 100vw, 307px\" \/><\/figure>\n\n\n\n<p>Correction progressive et explication du code<\/p>\n\n\n\n<p>Les fonctions utilis\u00e9es ne sont pas forc\u00e9ment cod\u00e9es de la m\u00eame mani\u00e8re<\/p>\n\n\n\n<video controls=\"\" width=\"640\" height=\"480\">\n  <source src=\"http:\/\/yb-isn.fr\/video-nsi\/soussuite0.mp4\" type=\"video\/mp4\">\n<\/video>\n\n\n\n<p>R\u00e9solution et test de la m\u00e9thode na\u00efve utilis\u00e9e<\/p>\n\n\n\n<video controls=\"\" width=\"640\" height=\"480\">\n  <source src=\"http:\/\/yb-isn.fr\/video-nsi\/soussuite2.mp4\" type=\"video\/mp4\">\n<\/video>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/posts\/253"}],"collection":[{"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":37,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":873,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/873"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yb-isn.fr\/2021\/nsi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}