par
Spols » 13 avr. 2022, 14:53
voici le code que j'ai adapté de cette page
https://codereview.stackexchange.com/qu ... from-array
<?php
function pc_permute($items, $perms = array( )) {
$back = array();
if (empty($items)) {
$back[] = "0$perms[0] 1$perms[1] 2$perms[2] 3$perms[3] 4$perms[4] 5$perms[5] 6$perms[6] 7$perms[7] 8$perms[8] 9$perms[9]";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$back = array_merge($back, pc_permute($newitems, $newperms));
}
}
return $back;
}
function pc_permute2($items) {
foreach($items as $k1 => $items1) {
foreach($items as $k2 => $items2) {
if ($k1 == $k2) continue;
foreach($items as $k3 => $items3) {
if ($k1 == $k3 || $k2 == $k3) continue;
foreach($items as $k4 => $items4) {
if ($k1 == $k4 ||$k2 == $k4 || $k3 == $k4) continue;
foreach($items as $k5 => $items5) {
if ($k1 == $k5 || $k2 == $k5 || $k3 == $k5 || $k4 == $k5) continue;
foreach($items as $k6 => $items6) {
if ($k1 == $k6 || $k2 == $k6 || $k3 == $k6 || $k4 == $k6 || $k5 == $k6 ) continue;
foreach($items as $k7 => $items7) {
if ($k1 == $k7 || $k2 == $k7 || $k3 == $k7 || $k4 == $k7 || $k5 == $k7 || $k6 == $k7) continue;
foreach($items as $k8 => $items8) {
if ($k1 == $k8 ||$k2 == $k8 || $k3 == $k8 ||$k4 == $k8 || $k5 == $k8 ||$k6 == $k8 || $k7 == $k8) continue;
foreach($items as $k9 => $items9) {
if ($k1 == $k9 || $k2 == $k9 || $k3 == $k9 || $k4 == $k9 || $k5 == $k9 || $k6 == $k9 || $k7 == $k9 || $k8 == $k9) continue;
foreach($items as $k10 => $items10) {
if ($k1 == $k10 || $k2 == $k10 || $k3 == $k10 || $k4 == $k10 || $k5 == $k10 || $k6 == $k10 || $k7 == $k10 || $k8 == $k10 || $k9 == $k10) continue;
$back[] = "0$items1 1$items2 2$items3 3$items4 4$items5 5$items6 6$items7 7$items8 8$items9 9$items10 ";
}
}
}
}
}
}
}
}
}
}
return $back;
}
//var_dump(pc_permute([0,1,2,3,4,5,6,7,8,9]));
var_dump(pc_permute2([0,1,2,3,4,5,6,7,8,9]));
J'ai codé aussi la solution proposée (on liste tous et on exclu les mauvais) mais j'ai pas comparer les temps d'éxécution.
Par contre j'ai du monter la mémoire à 1G pour y arriver.
Si tu ne cherches qu'a obtenir une des lignes aléatoirement, ca simplifie le problème
voici le code que j'ai adapté de cette page https://codereview.stackexchange.com/questions/73749/generate-every-unique-combination-using-all-elements-from-array
[php]<?php
function pc_permute($items, $perms = array( )) {
$back = array();
if (empty($items)) {
$back[] = "0$perms[0] 1$perms[1] 2$perms[2] 3$perms[3] 4$perms[4] 5$perms[5] 6$perms[6] 7$perms[7] 8$perms[8] 9$perms[9]";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$back = array_merge($back, pc_permute($newitems, $newperms));
}
}
return $back;
}
function pc_permute2($items) {
foreach($items as $k1 => $items1) {
foreach($items as $k2 => $items2) {
if ($k1 == $k2) continue;
foreach($items as $k3 => $items3) {
if ($k1 == $k3 || $k2 == $k3) continue;
foreach($items as $k4 => $items4) {
if ($k1 == $k4 ||$k2 == $k4 || $k3 == $k4) continue;
foreach($items as $k5 => $items5) {
if ($k1 == $k5 || $k2 == $k5 || $k3 == $k5 || $k4 == $k5) continue;
foreach($items as $k6 => $items6) {
if ($k1 == $k6 || $k2 == $k6 || $k3 == $k6 || $k4 == $k6 || $k5 == $k6 ) continue;
foreach($items as $k7 => $items7) {
if ($k1 == $k7 || $k2 == $k7 || $k3 == $k7 || $k4 == $k7 || $k5 == $k7 || $k6 == $k7) continue;
foreach($items as $k8 => $items8) {
if ($k1 == $k8 ||$k2 == $k8 || $k3 == $k8 ||$k4 == $k8 || $k5 == $k8 ||$k6 == $k8 || $k7 == $k8) continue;
foreach($items as $k9 => $items9) {
if ($k1 == $k9 || $k2 == $k9 || $k3 == $k9 || $k4 == $k9 || $k5 == $k9 || $k6 == $k9 || $k7 == $k9 || $k8 == $k9) continue;
foreach($items as $k10 => $items10) {
if ($k1 == $k10 || $k2 == $k10 || $k3 == $k10 || $k4 == $k10 || $k5 == $k10 || $k6 == $k10 || $k7 == $k10 || $k8 == $k10 || $k9 == $k10) continue;
$back[] = "0$items1 1$items2 2$items3 3$items4 4$items5 5$items6 6$items7 7$items8 8$items9 9$items10 ";
}
}
}
}
}
}
}
}
}
}
return $back;
}
//var_dump(pc_permute([0,1,2,3,4,5,6,7,8,9]));
var_dump(pc_permute2([0,1,2,3,4,5,6,7,8,9]));[/php]
J'ai codé aussi la solution proposée (on liste tous et on exclu les mauvais) mais j'ai pas comparer les temps d'éxécution.
Par contre j'ai du monter la mémoire à 1G pour y arriver.
Si tu ne cherches qu'a obtenir une des lignes aléatoirement, ca simplifie le problème