phpOMS/Math/Permutation.php
Dennis Eichhorn 78bc28b045 Draft
All these files need further edits + other optimization files + moving
of files
2016-03-13 21:49:01 +01:00

24 lines
603 B
PHP

<?php
class Permutation
{
// usage: permut(['a', 'b', 'c']);
public static function permut(array $toPermute, array $result = []) : array
{
$permutations = [];
if(empty($toPermute)){
$permutations[] = implode("", $result);
}else{
foreach($toPermute as $key => $val){
$newArr = $toPermute;
$newres = $result;
$newres[] = $val;
unset($newArr[$key]);
$permutations += permut($newArr, $newres);
}
}
return $permutations;
}
}