Dee*_*ele 33 php permutation combinatorics
我有数字,从0到8.我想在结果中,所有可能的那些数字组,每组应该使用所有数字,每个数字只能在一组中出现一次.
我想在PHP中看到可以打印出结果的解决方案.或者,至少,我想在组合学理论上有一些更新,因为我早就忘记了它.计算有多少排列的公式是什么?
示例集:
yzx*_*ben 50
您正在寻找排列公式:
nPk = n!/(n-k)!
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您有9个条目,并且您想要选择所有条目,即9P9 = 9!= 362880
你可以在O'Reilly的"PHP Cookbook"的食谱4.26中找到一个PHP算法来排列.
pc_permute(array(0, 1, 2, 3, 4, 5, 7, 8));
Run Code Online (Sandbox Code Playgroud)
从O'Reilly复制:
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
print join(' ', $perms) . "\n";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
Run Code Online (Sandbox Code Playgroud)
spe*_*nta 30
从PHP 5.5开始,您可以使用Generators.生成器可以节省大量内存并且速度更快(与pc_permute()相比超过一半).因此,如果您有机会安装PHP 5.5,那么您肯定需要Generators.这段剪辑是从Python移植的:https://stackoverflow.com/a/104436/3745311
function permutations(array $elements)
{
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield array_merge(
array_slice($permutation, 0, $i),
[$elements[0]],
array_slice($permutation, $i)
);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
样品用法:
$list = ['a', 'b', 'c'];
foreach (permutations($list) as $permutation) {
echo implode(',', $permutation) . PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)
输出:
a,b,c
b,a,c
b,c,a
a,c,b
c,a,b
c,b,a
Run Code Online (Sandbox Code Playgroud)
小智 23
由于此问题经常出现在Google搜索结果中,因此这是已接受答案的修改版本,它返回数组中的所有组合并将它们作为函数的返回值传递.
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
$return = array($perms);
} else {
$return = array();
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
$return = array_merge($return, pc_permute($newitems, $newperms));
}
}
return $return;
}
Run Code Online (Sandbox Code Playgroud)
使用:
$value = array('1', '2', '3');
print_r(pc_permute($value));
Run Code Online (Sandbox Code Playgroud)
Pio*_*iak 10
我有一些你可能会喜欢的东西
function combination_number($k,$n){
$n = intval($n);
$k = intval($k);
if ($k > $n){
return 0;
} elseif ($n == $k) {
return 1;
} else {
if ($k >= $n - $k){
$l = $k+1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $n-$k ; $i++)
$m *= $i;
} else {
$l = ($n-$k) + 1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $k ; $i++)
$m *= $i;
}
}
return $l/$m;
}
function array_combination($le, $set){
$lk = combination_number($le, count($set));
$ret = array_fill(0, $lk, array_fill(0, $le, '') );
$temp = array();
for ($i = 0 ; $i < $le ; $i++)
$temp[$i] = $i;
$ret[0] = $temp;
for ($i = 1 ; $i < $lk ; $i++){
if ($temp[$le-1] != count($set)-1){
$temp[$le-1]++;
} else {
$od = -1;
for ($j = $le-2 ; $j >= 0 ; $j--)
if ($temp[$j]+1 != $temp[$j+1]){
$od = $j;
break;
}
if ($od == -1)
break;
$temp[$od]++;
for ($j = $od+1 ; $j < $le ; $j++)
$temp[$j] = $temp[$od]+$j-$od;
}
$ret[$i] = $temp;
}
for ($i = 0 ; $i < $lk ; $i++)
for ($j = 0 ; $j < $le ; $j++)
$ret[$i][$j] = $set[$ret[$i][$j]];
return $ret;
}
Run Code Online (Sandbox Code Playgroud)
以下是如何使用它:
要获得组合数量:
combination_number(3,10); // returns number of combinations of ten-elements set.
Run Code Online (Sandbox Code Playgroud)
要获得所有可能的组合:
$mySet = array("A","B","C","D","E","F");
array_combination(3, $mySet); // returns all possible combinations of 3 elements of six-elements set.
Run Code Online (Sandbox Code Playgroud)
希望你能利用它.
这是我的课程版本.此类构建并返回置换数组作为结果
class Permutation {
private $result;
public function getResult() {
return $this->result;
}
public function permute($source, $permutated=array()) {
if (empty($permutated)){
$this->result = array();
}
if (empty($source)){
$this->result[] = $permutated;
} else {
for($i=0; $i<count($source); $i++){
$new_permutated = $permutated;
$new_permutated[] = $source[$i];
$new_source = array_merge(array_slice($source,0,$i),array_slice($source,$i+1));
$this->permute($new_source, $new_permutated);
}
}
return $this;
}
}
$arr = array(1,2,3,4,5);
$p = new Permutation();
print_r($p->permute($arr)->getResult());
Run Code Online (Sandbox Code Playgroud)
最后三行来测试我的课程.
我已经移植了此处列出的Python itertools代码(使用生成器)。与到目前为止发布的解决方案相比,优点是它允许您指定r(排列大小)。
function permutations($pool, $r = null) {
$n = count($pool);
if ($r == null) {
$r = $n;
}
if ($r > $n) {
return;
}
$indices = range(0, $n - 1);
$cycles = range($n, $n - $r + 1, -1); // count down
yield array_slice($pool, 0, $r);
if ($n <= 0) {
return;
}
while (true) {
$exit_early = false;
for ($i = $r;$i--;$i >= 0) {
$cycles[$i]-= 1;
if ($cycles[$i] == 0) {
// Push whatever is at index $i to the end, move everything back
if ($i < count($indices)) {
$removed = array_splice($indices, $i, 1);
array_push($indices, $removed[0]);
}
$cycles[$i] = $n - $i;
} else {
$j = $cycles[$i];
// Swap indices $i & -$j.
$i_val = $indices[$i];
$neg_j_val = $indices[count($indices) - $j];
$indices[$i] = $neg_j_val;
$indices[count($indices) - $j] = $i_val;
$result = [];
$counter = 0;
foreach ($indices as $indx) {
array_push($result, $pool[$indx]);
$counter++;
if ($counter == $r) break;
}
yield $result;
$exit_early = true;
break;
}
}
if (!$exit_early) {
break; // Outer while loop
}
}
}
Run Code Online (Sandbox Code Playgroud)
它对我有用,但是没有希望!用法示例:
$result = iterator_to_array(permutations([1, 2, 3, 4], 3));
foreach ($result as $row) {
print implode(", ", $row) . "\n";
}
Run Code Online (Sandbox Code Playgroud)