Skip to content

Commit

Permalink
Create vandresca.php
Browse files Browse the repository at this point in the history
  • Loading branch information
vandresca authored Sep 4, 2023
1 parent 061f4d6 commit 66f6cd6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Retos/Reto #36 - PERMUTACIONES [Media]/php/vandresca.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* Crea un programa que sea capaz de generar e imprimir todas las
* permutaciones disponibles formadas por las letras de una word.
* - Las words generadas no tienen por qué existir.
* - Deben usarse todas las letras en cada permutación.
* - Ejemplo: sol, slo, ols, osl, los, lso
*/

function permutations($word){
if(strlen($word)==0){
return [""];
}

$permutations = array();
$wordLength=strlen($word);
for($i=0; $i<$wordLength; $i++){
foreach(permutations(substr($word, 0, $i) . substr($word, $i + 1)) as $key=>$permutation) {
$permutations[] = $word[$i] . $permutation;
}
}
return $permutations;
}

function printPermutations($word){
$permutations = permutations($word);
foreach($permutations as $permutation){
echo $permutation. " ";
}
echo "\n";
}

printPermutations("sol");
printPermutations("mar");
printPermutations("cabra");

?>

0 comments on commit 66f6cd6

Please sign in to comment.