修复置换输出

时间:2011-10-19 20:13:34

标签: php permutation

嘿那里我已经为类做了一个递归排列函数,但是输出不太有利。 http://codepad.org/DOaMP9oc

function permute($arr) {
    $out = array();
    if (count($arr) > 1) {
        $i = 0;
        foreach($arr as $r => $c) {
            $n = $arr;
            unset($n[$r]);
            $out[$c] = permute($n);
        }
    }
    else
        return array_shift($arr);
    return $out;
}

如果输入为array(1,2,3,4,5),则输出为:

Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [3] => Array
                        (
                            [4] => 5
                            [5] => 4
                        )

                    [4] => Array
                        (
                            [3] => 5
                            [5] => 3
                        )

                    [5] => Array
                        (
                            [3] => 4
                            [4] => 3
                        )

                )
ETC......................

这是正确的 ,您可以像这个key.key.key.key.value或1234512354一样阅读, 12435

目前,要将此输出转换为可读的内容,我正在使用这个丑陋的代码块: http://codepad.org/qyWcRBCl

foreach($out as $k => $a) 
    foreach($a as $l => $b) 
        foreach ($b as $m => $c) 
            foreach ($c as $n => $d) 
                echo $k.$l.$m.$n.$d.'<br>';

如何更改我的功能以消除foreach堆栈并以permute()的类似格式输出。

4 个答案:

答案 0 :(得分:1)

我的解决方案是处理字符串:

function permute($string) {
    if (strlen($string)<2) {
        return array($string);
    }

    $permutations = array();

    // Copy everything but the first character of the string.
    $copy = substr($string, 1);

    foreach (permute($copy) as $permutation) {
        $length = strlen($permutation);

        // Insert the first character of the original string.
        for ($i=0; $i<=$length; $i++) {
            $permutations[] = substr($permutation, 0, $i) . $string[0] . substr($permutation, $i);
        }
    }

    sort($permutations);

    return $permutations;
}

header('Content-type:text/plain');
print_r(permute('12345'));

你已经有了一个有效的实施方案,所以我毫不犹豫地给你。请注意,数组不是按顺序创建的,所以我只是在最后对它进行排序。另请注意,这仅适用于您打算使用1个字符值的内容,因此对汽车名称进行排列是行不通的。


即使您不喜欢这个答案,我建议您对数组使用类型提示:

function permute(array $arr) {

这将强制您将数组传入其中。

答案 1 :(得分:0)

function display_permutation($array){
 if(is_array($array)){
   foreach($array as $key => $val){
     echo $key;
 display_permutation($val);
   }
 }else{
echo $array;
 }
}

答案 2 :(得分:0)

这是我的排列功能,我们可以用简单的方式显示结果

class Permute {
    public $results = Array();
    public function __construct(array $array) {
        $this->_permute($array);
    }

    private function _permute(array $orig, array $perm = Array()) {
        if(!count($orig)) {
            $this->results[] = $perm;
            return null;
        }
        $count = count($orig);
        for($i = 0; $i < $count; ++$i) {
            $orig2 = $orig;
            unset($orig2[$i]);
            $orig2 = array_values($orig2);
            $perm2 = $perm;
            $perm2[] = $orig[$i];
            $this->_permute($orig2, $perm2);
        }
    }
}


$arr = Array(1,2,3,4,5);
$permute = new Permute($arr);

foreach($permute->results as $result) {
    echo join('', $result).'<br>';
}

答案 3 :(得分:0)

我选择使用以下功能:

function showPerms($a,$i='') {
    if (is_array($a))
        foreach($a as $k => $v) 
            showPerms($v,$i.$k);
    else 
        echo $i.$a."\n";
}                   

但是,我还是喜欢用单一的递归函数来做这件事。