0
Ladies and gentlemen, I would like to draw up an anagram of the possible combinations. Ex: "123456" would generate: 1234 2341 6531 5431 1243 1342 or 12 43 56 23 14 16, whether or not repeated, and so on, I know how to get the number of combinations by factorial, but I don’t know how to start the algorithm to generate the combinations, I’ve read enough about combinatorial analysis, permutation etc, but I haven’t been able to implement the algorithm yet. If anyone could give me just one light to start would be GREAT! may be in pseudocode even.
would pass a String and size. Ex: where size would be ten hundred or thousand.
class Permutacao
{
public $resul;
private $cont;
function __construct()
{
$this->resul = array();
}
public function permuta($array, $indice)
{
if($indice == count($array)){
$this->cont++;
array_push($this->resul, $this->arraytemp);
}
else{
for ($i=0; $i < count($array); $i++) {
$valida = false;
for ($j=0; $j < $indice; $j++) {
if($this->arraytemp[$j] == $array[$i]) $valida = true;
}
if (!$valida) {
echo $indice." ";
$this->arraytemp[$indice] = $array[$i];
$this->permuta($array, $indice + 1);
}
}
}
}
}
echo "<pre>";
$permuta = new Permutacao();
$permuta->permuta(array(1,2,3,4), 0);
print_r($permuta->resul);
UPDATE: Searching I found this simplest way to implement, my biggest difficulty is to understand the algorithm, as I pass the "$Indice = 0" it reaches 3 and then returns to 1 etc., and if you pass the array with repeated values it does not return anything! 1,1,2,3 , he would have to return
1,1,3
1,1,2
2,1,1 etc.
Now yes, this is a valid answer :) . You can delete the other one, since you were able to adapt to php.
– user28595