I am using someones permutation solution in PHP that was given off stack and was wondering if there was a way for me to limit the character count in the string to be a fixed amount? Lets say I want to limit the string length to only 4 characters. To be honest, I'm not sure really whats going on in this formula and was hoping I could modify it, but I would also be interested in a new formula approach as well especially if its faster from another individual. Thanks
ini_set('memory_limit', '1024M');
function permutations(array $elements)
{
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield array_merge(
array_slice($permutation, 0, $i),
[$elements[0]],
array_slice($permutation, $i)
);
}
}
}
}
$list = ['a', 'b', 'c', 'd', 'e', 'f'];
$newlist = array();
foreach (permutations($list) as $permutation) {
//echo implode(',', $permutation) . PHP_EOL;
array_push($newlist, implode(',', $permutation));
}
echo sizeof($newlist);
echo sizeof($newlist);to print the size which is probably 6! for['a', 'b', 'c', 'd', 'e', 'f'].Lets say I want to limit the string length to only 4 characters? Can you give an example?