【问题标题】:Permutation and Combination in PhPPhP中的排列组合
【发布时间】:2020-08-12 08:24:25
【问题描述】:

我想从数组 [1,2,3,4,5] 中计算排列 5p2、5p3、5p4 和 5p5 下面的函数只运行 5p5。要运行 5p2、5p3、5p4,我必须使用 for...循环手动遍历数组。 请帮帮我。

//function to return permutations 5p5 array
        function combinationArray($myarr) {
            $results = [];
            $current = [];
            $next = function($myarr) use(&$next,&$results,&$current) {
                $l = count($myarr);
                if($l === 1) {
                    $current []= $myarr[0];
                    $results []= intval(implode($current)); //use this for array containing numbers only
                    //$results []= implode($current); //use this for array containing alphabets
                    array_pop($current);
                    return;
                }
                for($i=0; $i<$l; $i++) {
                    $tmpArr = $myarr;
                    $current []= array_splice($tmpArr,$i,1)[0];
                    $next($tmpArr);
                    array_pop($current);
                }
            };
            $next($myarr);
            return $results;
        }

//5c2的手动迭代会是这样的......大约120个不同的模块......

$wordArray = str_split($sWord, 1);
$newArray1 = $wordArray[0]; $newArray2 = $wordArray[1];$newArray3 = $wordArray[2]; $newArray4 = $wordArray[3]; $newArray5 = $wordArray[4]; 
    //12
for($a = 0; $a < count($newArray1); $a++){
    for($b = 0; $b < count($newArray2); $b++){
        $aux = $newArray1[$a].$newArray2[$b]; array_push($result52, $aux);                              
    }
}
//13
for($a = 0; $a < count($newArray1); $a++){
    for($c = 0; $c < count($newArray3); $c++){
        $aux = $newArray1[$a].$newArray3[$c]; array_push($result52, $aux);                              
    }
}
//14
for($a = 0; $a < count($newArray1); $a++){
    for($d = 0; $d < count($newArray4); $d++){
        $aux = $newArray1[$a].$newArray4[$d]; array_push($result52, $aux);                              
    }
}
//15
for($a = 0; $a < count($newArray1); $a++){
    for($e = 0; $e < count($newArray5); $e++){
        $aux = $newArray1[$a].$newArray5[$e]; array_push($result52, $aux);                              
    }
}
//21
for($b = 0; $b < count($newArray2); $b++){
    for($a = 0; $a < count($newArray1); $a++){      
        $aux = $newArray2[$b].$newArray1[$a]; array_push($result52, $aux);                              
    }
}
//23
for($b = 0; $b < count($newArray2); $b++){
    for($c = 0; $c < count($newArray3); $c++){      
        $aux = $newArray2[$b].$newArray3[$c]; array_push($result52, $aux);                              
    }
}
//24
for($b = 0; $b < count($newArray2); $b++){
    for($d = 0; $d < count($newArray4); $d++){      
        $aux = $newArray2[$b].$newArray4[$d]; array_push($result52, $aux);                              
    }
}
//25
for($b = 0; $b < count($newArray2); $b++){
    for($e = 0; $e < count($newArray5); $e++){  
        $aux = $newArray2[$b].$newArray5[$e]; array_push($result52, $aux);                              
    }
}
//31
for($c = 0; $c < count($newArray3); $c++){
    for($a = 0; $a < count($newArray1); $a++){
        $aux = $newArray3[$c].$newArray1[$a]; array_push($result52, $aux);                              
    }
}
//32
for($c = 0; $c < count($newArray3); $c++){
    for($b = 0; $b < count($newArray2); $b++){
        $aux = $newArray3[$c].$newArray2[$b]; array_push($result52, $aux);                              
    }
}
//34
for($c = 0; $c < count($newArray3); $c++){
    for($d = 0; $d < count($newArray4); $d++){
        $aux = $newArray3[$c].$newArray4[$d]; array_push($result52, $aux);                              
    }
}
//35
for($c = 0; $c < count($newArray3); $c++){
    for($e = 0; $e < count($newArray5); $e++){
        $aux = $newArray3[$c].$newArray5[$e]; array_push($result52, $aux);                              
    }
}
//41
for($d = 0; $d < count($newArray4); $d++){
    for($a = 0; $a < count($newArray1); $a++){
        $aux = $newArray4[$d].$newArray1[$a]; array_push($result52, $aux);                              
    }
}
//42
for($d = 0; $d < count($newArray4); $d++){
    for($b = 0; $b < count($newArray2); $b++){
        $aux = $newArray4[$d].$newArray2[$b]; array_push($result52, $aux);                              
    }
}
//43
for($d = 0; $d < count($newArray4); $d++){
    for($c = 0; $c < count($newArray3); $c++){
        $aux = $newArray4[$d].$newArray3[$c]; array_push($result52, $aux);                              
    }
}
//45
for($d = 0; $d < count($newArray4); $d++){
    for($e = 0; $e < count($newArray5); $e++){
        $aux = $newArray4[$d].$newArray5[$e]; array_push($result52, $aux);                              
    }
}
//51
for($e = 0; $e < count($newArray5); $e++){
    for($a = 0; $a < count($newArray1); $a++){
        $aux = $newArray5[$e].$newArray1[$a]; array_push($result52, $aux);                              
    }
}
//52
for($e = 0; $e < count($newArray5); $e++){
    for($b = 0; $b < count($newArray2); $b++){
        $aux = $newArray5[$e].$newArray2[$b]; array_push($result52, $aux);                              
    }
}
//53
for($e = 0; $e < count($newArray5); $e++){
    for($c = 0; $c < count($newArray3); $c++){
        $aux = $newArray5[$e].$newArray3[$c]; array_push($result52, $aux);                              
    }
}
//54
for($e = 0; $e < count($newArray5); $e++){
    for($d = 0; $d < count($newArray4); $d++){
        $aux = $newArray5[$e].$newArray4[$d]; array_push($result52, $aux);                              
    }
}

$result52 = array_unique($result52); //remove duplicates... but the indexes dont align.
$result52 = array_reduce($result52,"myfunction");//convert the array into a string bcos of the indexes              
$result52 = str_split($result52, 2); //split the new string in twos into an array to get correct indexes
print_r($result52);

谢谢大家。

【问题讨论】:

  • 你的问题是什么?例如。你需要什么帮助?
  • 我需要帮助计算数组 [1,2,3,4,5] 中的排列 5p2、5p3、5p4,而不是手动计算
  • 这就是循环的用途——遍历事物列表直到满足条件,有意或无意。只要您想对事物列表执行某种计算,就必须遍历它们。我可能把你的问题读错了,但这是我根据我的理解认为的
  • 那么,您是否得出结论,对于 20 种不同的组合,没有其他方法可以在 5 项数组中置换 5c2?
  • 我确定有。我自己的某些东西尖叫着递归,但我目前正在工作,所以我现在不能深入研究这个问题,抱歉......

标签: php arrays permute


【解决方案1】:

我在网上钓到了一些东西。我希望这能满足您的需求。

我对此不以为然。它是这两种解决方案的组合:Generating a Power SetImplementing Heap's Algorithm

<?php

$arr1 = [1,2,3];

/**
 * Permutation.
 */
class Permutation
{
    /**
     * @var array
     */
    protected $items;

    /**
     * @var array
     */
    protected $result = [];

    /**
     * @param array $array
     * @param int   $permutation
     */
    public function __construct($array, $permutation)
    {
        if (count($array) < $permutation) {
            throw new Exception("That's impossible for me :/");
        }

        // first get all the unique subsets with the permutation's length
        $subsets = $this->getPowerSet($array, $permutation);

        // genereate the permutation for each subset
        foreach ($subsets as $subset) {
            $this->setItems($subset)->heaps($this->n, $this->items);
        }

        sort($this->result);
    }

    /**
     * Get result.
     *
     * @return array
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * Set items.
     *
     * @param array $items
     */
    public function setItems($items)
    {
        $this->items = $items;
        $this->n = count($items);
        return $this;
    }

    private function swap(&$items, $i, $j)
    {
        $temp = $items[$i];
        $items[$i] = $items[$j];
        $items[$j] = $temp;
    }

    /**
     * This is Heap's algorithm, generates all possible permutations of n items.
     *
     * @link https://en.wikipedia.org/wiki/Heap%27s_algorithm
     *
     * @param int   $n
     * @param array $items
     */
    private function heaps($n, &$items)
    {
        if ($n == 1) {
            $this->result[] = implode('', $items);
        } else {
            for ($i = 0; $i < $n; ++$i) {
                $this->heaps($n - 1, $items);
                if ($n % 2 == 0) {
                    $this->swap($items, 0, $n - 1);
                } else {
                    $this->swap($items, $i, $n - 1);
                }
            }
        }
    }

    /**
     * Way to get the Power Set (set of all unique subsets)
     * 
     * @link https://www.php.net/manual/en/function.shuffle.php#88408
     * 
     * @param array $in
     * @param int   $length
     */
    private function getPowerSet($in, $length) {
        $count = count($in);
        $members = pow(2,$count);
        $return = array();
        for ($i = 0; $i < $members; $i++) {
            $b = sprintf("%0".$count."b",$i);
            $out = array();
            for ($j = 0; $j < $count; $j++) {
                if ($b{$j} == '1') $out[] = $in[$j];
            }
            if (count($out) === $length) {
                $return[] = $out;
            }
        }
        return $return;
    }
}

$permutation = new Permutation($arr1, 2);

echo "<pre>";
print_r($permutation->getResult());
echo "</pre>";

【讨论】:

  • 嗨,Argee...再见。上面的排列算法会返回重复项……尤其是在 2 个数字重复的情况下。并且,计算 5, 6,7....n 项,返回三次到四次。我使用 array_unique() 删除重复项,但最终删除了主数组中的所有项目......留下一个空数组。请问这个怎么解决?
  • 我再次检查,重复的已被删除,返回分散的索引。我需要将索引重置为串行...请问我该怎么做?
  • 重复是什么意思?如果你有 12 和 21,你认为这是重复的吗?如果是这样,您不需要排列而是组合。在数学上有一个区别:“排列用于列表(顺序很重要),组合用于组(顺序无关紧要)”来自betterexplained.com/articles/easy-permutations-and-combinations
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
相关资源
最近更新 更多