{"id":1930,"date":"2024-09-08T17:25:42","date_gmt":"2024-09-08T16:25:42","guid":{"rendered":"https:\/\/blog.univ-angers.fr\/mathsinfo\/?p=1930"},"modified":"2024-09-08T17:52:32","modified_gmt":"2024-09-08T16:52:32","slug":"amusons-nous-avec-des-mots-du-dictionnaire","status":"publish","type":"post","link":"https:\/\/blog.univ-angers.fr\/mathsinfo\/2024\/09\/08\/amusons-nous-avec-des-mots-du-dictionnaire\/","title":{"rendered":"Amusons-nous avec des mots du dictionnaire"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Exercice 1<\/h3>\n\n\n\n<p>\u00c9crire un programme qui cherche tous les <strong>mots de 5 lettres<\/strong> pouvant \u00eatre form\u00e9s <strong>\u00e0 partir de mots de 9 lettres<\/strong> en prenant <strong>1 lettre sur 2<\/strong>, en commen\u00e7ant par la premi\u00e8re lettre. Exemples :<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"412\" height=\"1024\" src=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-1-412x1024.png\" alt=\"\" class=\"wp-image-1931\" style=\"width:250px;height:auto\" srcset=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-1-412x1024.png 412w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-1-121x300.png 121w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-1.png 444w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/a><figcaption class=\"wp-element-caption\">Une lettre sur deux<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Vous pouvez utiliser ces 2 dictionnaires (mots de 9 et 5 lettres) : <a href=\"https:\/\/uabox.univ-angers.fr\/s\/b3GotFzpTYkL4MT\">https:\/\/uabox.univ-angers.fr\/s\/b3GotFzpTYkL4MT<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Corrig\u00e9 en python<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open('dictionary_9.txt', 'r') as f:\n    dictionary_9 = &#091;line.strip() for line in f]\n\nwith open('dictionary_5.txt', 'r') as f:\n    dictionary_5 = set(line.strip() for line in f)\n\ncompte = 0                      # Nb de mots trouv\u00e9s\n\nfor word9 in dictionary_9:\n  word5 = word9&#091;::2]            # une lettre sur deux\n  if word5 in dictionary_5:     # Si le mot existe\n    print(word9, word5)         # on l'affiche\n    compte += 1                 # et le compteur augmente de +1\nprint(f\"Total = {compte}\")  <\/code><\/pre>\n\n\n\n<p>R\u00e9sultat :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ABLATIONS ALTOS\nABLUTIONS ALTOS\nABREGEAIS ARGAS\nABROGEAIS ARGAS\nABSTIENNE ASINE\nACCONIERS ACNES\n...\nVIELLEUSE VELUE\nVIELLIONS VELOS\nVIENDRAIS VEDAS\nVOILETTES VIETS\nTotal = 913<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">corrig\u00e9 en javascript<\/h3>\n\n\n\n<p><a href=\"https:\/\/difabiolorenzo.github.io\/motusJS\/\">Cliquez sur ce lien<\/a> puis bouton droit &#8211; Inspecter &#8211; Console. Copiez-collez le code suivant :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> dictionary_9.reduce((a, m) =&gt; {\n     var mot5 = m.slice(0,2)+m&#091;4]+m.slice(-2);\n     return dictionary_5.includes(mot5) ? &#091;...a, &#091;m, mot5]] : a \n  }, &#091;])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Exercice 2<\/h3>\n\n\n\n<p>Identifiez tous les <strong>mots de 10 lettres<\/strong> qui peuvent \u00eatre <strong>compos\u00e9s de 2 mots de 5 lettres<\/strong> en <strong>respectant l&rsquo;ordre<\/strong> des lettres. <span style=\"text-decoration: underline\">Exemple<\/span> : <strong>RACHIDIENS<\/strong> s&rsquo;\u00e9crit \u00e0 partir de <strong>CHIEN<\/strong> et <strong>RADIS<\/strong><\/p>\n\n\n\n<p>Comme il y a beaucoup de solutions, on peut cr\u00e9er une fonction qui admet en param\u00e8tre un mot de 5 lettres et qui renvoie tous les mots de 10 lettres contenant ce mot ainsi que l&rsquo;autre mot de 5 lettres pour compl\u00e9ter. Exemples :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; trouve(\"CHIEN\")\nBANCHAIENT CHIEN BANAT\nCHARMAIENT CHIEN ARMAT\nCHERRAIENT CHIEN ERRAT\nCHIADERENT CHIEN ADRET\nCHIENDENTS CHIEN DENTS\nCHTHONIENS CHIEN THONS\nCHTONIENNE CHIEN TONNE\nCRASHAIENT CHIEN RASAT\nDOUCHAIENT CHIEN DOUAT\nLOUCHAIENT CHIEN LOUAT\nMATCHAIENT CHIEN MATAT\nRACHIDIENS CHIEN RADIS\nTOUCHAIENT CHIEN TOUAT\nTRICHAIENT CHIEN TRIAT<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Corrig\u00e9 en python<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open('dictionary_10.txt', 'r') as f:\n    dictionary_10 = &#091;line.strip() for line in f]\n\nwith open('dictionary_5.txt', 'r') as f:\n    dictionary_5 = set(line.strip() for line in f)\n\ndef is_included(word1, word2):\n    positions = &#091;]\n    index = 0\n    for letter in word2:\n        index = word1.find(letter, index)\n        if index == -1: return False\n        positions.append(index)\n        index += 1      \n    return positions\n\ndef trouve(word5):\n for word10 in dictionary_10:\n  positions = is_included(word10, word5)  \n  if positions:\n    reste = ''.join(word10&#091;i] for i in range(10) if i not in positions)\n    if reste in dictionary_5:\n       print(word10, word5, reste)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">exercice 3<\/h3>\n\n\n\n<p>Ci-dessous un jeu tr\u00e8s simple trouv\u00e9 dans le journal TV T\u00e9l\u00e9 Z. Dans notre cas nous allons travailler avec des mots de 9 et 5 lettres.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"659\" src=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-1024x659.png\" alt=\"\" class=\"wp-image-1933\" srcset=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-1024x659.png 1024w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-300x193.png 300w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-768x494.png 768w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-1536x988.png 1536w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2-466x300.png 466w, https:\/\/blog.univ-angers.fr\/mathsinfo\/files\/2024\/09\/image-2.png 1816w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><span style=\"text-decoration: underline\">But<\/span> : cr\u00e9er un programme qui, \u00e0 partir d&rsquo;une liste de mots de 5 lettres, va rechercher autant de mots de 9 lettres les contenant.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; jeu(&#091;\"AIDER\",\"CAPTER\",\"GAINE\",\"LOGER\",\"FILES\"])\nPYRAMIDER AIDER PYR-M----\nCAPTERAIT CAPTER ------AIT\nGRAINIERS GAINE -R---I-RS\nPLONGEOIR LOGER P--N--OI-\nFILONIENS FILES ---ONI-N-<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">corrig\u00e9 en python<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>from random import choice\n\nwith open('dictionary_9.txt', 'r') as f:\n    dictionary_9 = &#091;line.strip() for line in f]\n\nwith open('dictionary_5.txt', 'r') as f:\n    dictionary_5 = set(line.strip() for line in f)\n\ndef is_included(word1, word2):\n    positions = &#091;]\n    index = 0\n    for letter in word2:\n        index = word1.find(letter, index)\n        if index == -1: return False\n        positions.append(index)\n        index += 1      \n    return positions\n\ndef trouve(word5):\n res = &#091;]         # On cherche toutes les solutions\n for word9 in dictionary_9:\n  positions = is_included(word9, word5)  \n  if positions: res.append((word9, positions))\n if len(res) &gt; 0: return choice(res)      # on renvoie une solution au hasard\n return False\n\ndef jeu(arr):\n for mot in arr:\n  r = trouve(mot)\n  if r:     # Si un mot de 9 lettres a \u00e9t\u00e9 trouv\u00e9\n   cache = ''.join(r&#091;0]&#091;i] if i not in r&#091;1] else '-' for i in range(9))\n   print(r&#091;0], mot, cache)\n  else: print(f\"Rien trouv\u00e9 pour {mot}\")    \n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Exercice 1 \u00c9crire un programme qui cherche tous les mots de 5 lettres pouvant \u00eatre form\u00e9s \u00e0 partir de mots de 9 lettres en prenant 1 lettre sur 2, en commen\u00e7ant par la premi\u00e8re lettre. Exemples : Vous pouvez utiliser &hellip; <a href=\"https:\/\/blog.univ-angers.fr\/mathsinfo\/2024\/09\/08\/amusons-nous-avec-des-mots-du-dictionnaire\/\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4913,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1930","post","type-post","status-publish","format-standard","hentry","category-non-classe"],"_links":{"self":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/users\/4913"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/comments?post=1930"}],"version-history":[{"count":3,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1930\/revisions"}],"predecessor-version":[{"id":1935,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/posts\/1930\/revisions\/1935"}],"wp:attachment":[{"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/media?parent=1930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/categories?post=1930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.univ-angers.fr\/mathsinfo\/wp-json\/wp\/v2\/tags?post=1930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}