Permutation

Platform:  PHP
Published  Oct 20, 2011
Updated  Jun 29, 2012
Permute an array of values.

Code

 


The nature of the input allows you to pass either a string or an int into the function as a singular value:

Code

 


I'd advise staying under 9 values, as the amount of permutations required is

Code

 

You can see a list of amounts here: http://www4d.wolframalpha.com/Calculate/MSP/MSP11619hfc32hdb0fb84f00002gb116g73ib5b7g0?MSPStoreType=image/gif&s=31&w=350&h=1008 class permutation {
public $permutations = array();
function __construct(array $arr) { $this->showPerms($this->permute($arr)); }
private function showPerms($a,$i='') {
if (is_array($a))
foreach($a as $k => $v)
$this->showPerms($v,$i.$k);
else
$this->results[] = $i.$a;
}
private function permute(array $arr) {
$out = array();
if (count($arr) > 1)
foreach($arr as $r => $c) {
$n = $arr;
unset($n[$r]);
$out[$c] = $this->permute($n);
}
else
return array_shift($arr);
return $out;
}
}

Comments

Sign in to comment.
sean   -  Jul 02, 2012
I'm wondering the same.
 Respond  
Hawkee   -  Jun 29, 2012
The code box has a max-height, so you're fine. Very helpful to see the results. I just wonder what this could be used for in a practical situation.
 Respond  
Korvin   -  Jun 29, 2012
it's 120 lines heh but sure
 Respond  
Hawkee   -  May 12, 2012
It would be nice to see the results of your first example in the description.
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.