recherche par arbre

This commit is contained in:
2025-04-30 02:10:53 +02:00
parent 88d95e42ad
commit 7aea2d9f99
6 changed files with 51 additions and 56 deletions

View File

@ -3,7 +3,6 @@
const MIN_LETTRES_MOT_1 = 2;
const MIN_LETTRES_MOT_2 = 1;
const MAX_MOTS = 100000;
$dico = [];
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
@ -38,28 +37,14 @@ foreach ($dico as $mot => $definition) {
$mots_de_n_lettres[$n][] = $mot;
}
function fisherYatesShuffle(&$items)
{
for ($i = count($items) - 1; $i > 0; $i--) {
$j = mt_rand(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
function mots_espaces($longueur)
{
global $mots_de_n_lettres;
global $dico;
$nb_mots = 0;
fisherYatesShuffle($mots_de_n_lettres[$longueur]);
foreach ($mots_de_n_lettres[$longueur] as $mot) {
yield $mot;
if (++$nb_mots >= MAX_MOTS) {
return;
}
}
for ($i = MIN_LETTRES_MOT_1; $longueur - $i - 1 >= MIN_LETTRES_MOT_2; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
@ -67,14 +52,8 @@ function mots_espaces($longueur)
if ($mot1 != $mot2) {
$dico["$mot1 $mot2"] = array_merge($dico[$mot1], $dico[$mot2]);
yield "$mot1 $mot2";
if (++$nb_mots >= MAX_MOTS) {
return;
}
$dico["$mot2 $mot1"] = array_merge($dico[$mot2], $dico[$mot1]);
yield "$mot2 $mot1";
if (++$nb_mots >= MAX_MOTS) {
return;
}
}
}
}