Created
December 10, 2012 20:27
-
-
Save jsifalda/4253142 to your computer and use it in GitHub Desktop.
Permutation function. See https://gist.github.com/4259637
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function factorial($int){ | |
| if($int < 2) { | |
| return 1; | |
| } | |
| for($f = 2; $int-1 > 1; $f *= $int--); | |
| return $f; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function perms($arr) { | |
| $p = array(); | |
| for ($i=0; $i < factorial(count($arr)); $i++) { | |
| $p[] = perm($arr, $i); | |
| } | |
| return $p; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| # Taken from http://php.net/manual/en/function.shuffle.php#90615 | |
| $in = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'); | |
| $power_perms = power_perms($in); | |
| echo count($power_perms); | |
| print_r($power_perms); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment