修复排列输出

发布于 2024-12-11 04:03:14 字数 1767 浏览 0 评论 0原文

嘿,我已经为类创建了一个递归排列函数,但我的输出不太令人满意。 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......................

This is all Correct,你可以像这个键一样读取它.key.key.key.value 或12345,12354,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() 类似的格式进行堆栈和输出。

Hey there I've made a recursive permutation function for class, but I output is less than favorable.
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;
}

If input is array(1,2,3,4,5), Output is:

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

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

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

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

This is all Correct, you can read it like this key.key.key.key.value or 12345,12354,12435

Currently, to convert this output to something readable, I'm using this ugly block of code:
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>';

How can I alter my function to eliminate the foreach stack and output in a similar format from permute().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兰花执着 2024-12-18 04:03:15

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

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) {

这将强制您向其中传递一个数组。

My solution is to work on strings:

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'));

You already have a working implementation so I have no qualms in giving that to you. Note that the array is not created in order, so I simply sort it at the end. Also note that this only works on things that you intend to have the value of 1 character, so doing a permutation of car names would not work.


Even if you don't like this answer, I suggest that you use a type-hint for array:

function permute(array $arr) {

This will enforce that you pass an array into it.

香草可樂 2024-12-18 04:03:15
function display_permutation($array){
 if(is_array($array)){
   foreach($array as $key => $val){
     echo $key;
 display_permutation($val);
   }
 }else{
echo $array;
 }
}
function display_permutation($array){
 if(is_array($array)){
   foreach($array as $key => $val){
     echo $key;
 display_permutation($val);
   }
 }else{
echo $array;
 }
}
半透明的墙 2024-12-18 04:03:15

这是我的排列函数,我们可以以简单的方式显示结果

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>';
}

Here is my permutation function and we can display results in simple way

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>';
}
那支青花 2024-12-18 04:03:15

我选择使用以下函数:

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

但是,我仍然想在单个递归函数中执行此操作。

I opted to use the following function:

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

However, I'd still like to do this in a singular recursive function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文