note 90615 added to function.shuffle
| From: | dirkdotaveryatgmail at osu1 dot php dot net | Date: | Thu, 30 Apr 2009 15:58:42 +0000 |
| Subject: | note 90615 added to function.shuffle | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154050@lists.php.net to get a copy of this message | ||
Building on examples by m227 and pineappleclock, here is a function that returns all permutations of
each set in the power set of an array of strings (instead of a string). Thanks for the great
examples!
<?php
/*
Use: $arr = power_perms($in);
Example:
$in = array("A","B","C");
$power_perms = power_perms($in);
Returns:
Array
(
[0] => Array
(
[0] => A
[1] => B
[2] => C
)
[1] => Array
(
[0] => A
[1] => C
[2] => B
)
[2] => Array
(
[0] => B
[1] => A
[2] => C
)
[3] => Array
(
[0] => B
[1] => C
[2] => A
)
[4] => Array
(
[0] => C
[1] => A
[2] => B
)
[5] => Array
(
[0] => C
[1] => B
[2] => A
)
[6] => Array
(
[0] => A
[1] => B
)
[7] => Array
(
[0] => B
[1] => A
)
[8] => Array
(
[0] => B
[1] => C
)
[9] => Array
(
[0] => C
[1] => B
)
[10] => Array
(
[0] => A
[1] => C
)
[11] => Array
(
[0] => C
[1] => A
)
[12] => Array
(
[0] => A
)
[13] => Array
(
[0] => B
)
[14] => Array
(
[0] => C
)
)
*/
function power_perms($arr) {
$power_set = power_set($arr);
$result = array();
foreach($power_set as $set) {
$perms = perms($set);
$result = array_merge($result,$perms);
}
return $result;
}
function power_set($in,$minLength = 1) {
$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) >= $minLength) {
$return[] = $out;
}
}
//usort($return,"cmp"); //can sort here by length
return $return;
}
function factorial($int){
if($int < 2) {
return 1;
}
for($f = 2; $int-1 > 1; $f *= $int--);
return $f;
}
function perm($arr, $nth = null) {
if ($nth === null) {
return perms($arr);
}
$result = array();
$length = count($arr);
while ($length--) {
$f = factorial($length);
$p = floor($nth / $f);
$result[] = $arr[$p];
array_delete_by_key($arr, $p);
$nth -= $p * $f;
}
$result = array_merge($result,$arr);
return $result;
}
function perms($arr) {
$p = array();
for ($i=0; $i < factorial(count($arr)); $i++) {
$p[] = perm($arr, $i);
}
return $p;
}
function array_delete_by_key(&$array, $delete_key, $use_old_keys = FALSE) {
unset($array[$delete_key]);
if(!$use_old_keys) {
$array = array_values($array);
}
return TRUE;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.34.64.230
----
Manual Page -- http://www.php.net/manual/en/function.shuffle.php
Edit -- https://master.php.net/note/edit/90615
Del: integrated -- https://master.php.net/note/delete/90615/integrated
Del: useless -- https://master.php.net/note/delete/90615/useless
Del: bad code -- https://master.php.net/note/delete/90615/bad+code
Del: spam -- https://master.php.net/note/delete/90615/spam
Del: non-english -- https://master.php.net/note/delete/90615/non-english
Del: in docs -- https://master.php.net/note/delete/90615/in+docs
Del: other reasons-- https://master.php.net/note/delete/90615
Reject -- https://master.php.net/note/reject/90615
Search -- https://master.php.net/manage/user-notes.php