Skip to content

Instantly share code, notes, and snippets.

@jordi-pascual
Created October 20, 2017 16:37
Show Gist options
  • Save jordi-pascual/457136724f757825b224a550ec76a651 to your computer and use it in GitHub Desktop.
Save jordi-pascual/457136724f757825b224a550ec76a651 to your computer and use it in GitHub Desktop.
Permutation
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment