1

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);
asked Jul 29, 2019 at 15:18
9
  • What's the purpose of this code? What does it give you as output? Commented Jul 29, 2019 at 15:23
  • @vivek_23 The output is a list of combinations from the $list variable that exist. So combinations such as a,b,c,e,d,f & a,b,c,d,f,e etc..... I think with those characters being checked alone its like 5000 possible combinations exist. Commented Jul 29, 2019 at 15:46
  • Ok, but all I see is the echo sizeof($newlist); to print the size which is probably 6! for ['a', 'b', 'c', 'd', 'e', 'f']. Commented Jul 29, 2019 at 15:49
  • Also, what do you mean by Lets say I want to limit the string length to only 4 characters? Can you give an example? Commented Jul 29, 2019 at 15:49
  • @vivek_23 I was just doing that sizeof function to see how big the list was getting :) Limited character length meaning like so instead of outputting a,b,c,d,e,f it will output a,b,c,d instead Commented Jul 29, 2019 at 15:54

1 Answer 1

1

Here is an adaptation of the function that allows you to put a limit on the number of characters as second argument.

You need to add an extra parameter to know how many remaining characters are allowed and decrement it on the recursive call :

function permutations(array $elements, $limit)
{
 if( $limit == 1 )
 {
 // No need to go deeper, return a list of all remaining letters
 foreach($elements as $element)
 yield array($element);
 }
 foreach($elements as $i => $element)
 {
 // compute all the permutions, without the elements at index i
 $sub_perms = permutations( array_merge(array_slice($elements, 0, $i), array_slice($elements, $i+1)), $limit-1);
 // list all the permutations with the currently selected element + all the possible combinations of $limit-1 letters of the others elements 
 foreach($sub_perms as $sub_perm)
 {
 yield array_merge(array($element), $sub_perm);
 }
 }
}

You can then call permutations($list, 2) or permutations($list, 4) to have all the permutations of 2 or 4 characters.

answered Jul 29, 2019 at 19:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.