Jump to content

filter for permutation


Alicia

Recommended Posts

Hi guys,

 

How can I actually filter the result of the script below so no permuted numbers will not be displayed again.

 

e.g : 11, 12, 22, 23, 32

the output should show : display 12 and dont display 21, display 23 but not 32 and etc.

 

please advise. thanks in advance.

 

 

<?
function permutations($letters,$num){ 
    $last = str_repeat($letters{0},$num); 
    $result = array(); 
    while($last != str_repeat(lastchar($letters),$num)){ 
        $result[] = $last; 
        $last = char_add($letters,$last,$num-1); 
    } 
    $result[] = $last; 
    return $result; 
} 
function char_add($digits,$string,$char){ 
    if($string{$char} <> lastchar($digits)){ 
        $string{$char} = $digits{strpos($digits,$string{$char})+1}; 
        return $string; 
    }else{ 
        $string = changeall($string,$digits{0},$char); 
        return char_add($digits,$string,$char-1); 
    } 
} 
function lastchar($string){ 
    return $string{strlen($string)-1}; 
} 
function changeall($string,$char,$start = 0,$end = 0){ 
    if($end == 0) $end = strlen($string)-1; 
    for($i=$start;$i<=$end;$i++){ 
        $string{$i} = $char; 
    } 
    return $string; 
} 

$number = '1234';
$digit = 2;
$Array=permutations($number,$digit); 
for($i=0 ; $i < count($Array) ; $i++) { 
// $tt1[]=$Array[$i];
echo "$i) " . $Array[$i] . "<BR>"; 
} 

$signature = implode(null, $signature);
    if(false === in_array($signature, $used)){
        array_push($used, $signature);
  
  $string = "$signature";


?>

Link to comment
https://forums.phpfreaks.com/topic/188138-filter-for-permutation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.