#! /usr/bin/php
<? 
#################
#pour afficher les permutations d'une chaîne de caractère fournie en ligne de commande
#source: http://www.webmasterworld.com/forum88/778.htm
#adapted fred radeff, www.akademia.ch 25 nov 2008
#################
print "Input:\n";
$name = trim(fgets(STDIN));
#print "hello $name !";
$test="";
for($i=0;$i<strlen($name);$i++){
#echo substr($name,$i,1).";";
$test.=substr($name,$i,1).";";
}
$test=ereg_replace(";$","",$test);
#echo "\ntest input: " .$test;

$yourArr=explode(";",$test);
 
$n=count($yourArr);
 
 
for ($i=0; $i <= $n; $i++) $pArr[$i]=$i; //The permutation array.
 
function PrintPerm()
{
global $yourArr,$pArr,$n;
 
for ($i=1; $i <= $n; $i++) echo $yourArr[$pArr[$i]-1];
#echo "<br>";
echo "\n";
 
return;
}
 
function swapThem($i,$j)
{
global $pArr;
 
$temp = $pArr[$i];
$pArr[$i] = $pArr[$j];
$pArr[$j] = $temp;
}
 
function NextPerm()
{
global $pArr,$n;
 
$k = $n-1;
while ($pArr[$k] > $pArr[$k+1]) $k--;
if ($k == 0) return(0);
else
{
$j = $n;
while ($pArr[$k] > $pArr[$j]) $j--;
swapThem($j,$k);
$r = $n;
$s = $k+1;
while ($r > $s)
{
swapThem($r,$s);
$r--;
$s++;
}
}
PrintPerm();
return(1);
}
 
//Print the array values
PrintPerm();
while (NextPerm()); //Permute and print
?> 
 
info/permutations2.txt · Dernière modification: 2008/11/25 11:08 (modification externe)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki