Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
I have letters in a string, I would like in result get all possible sets of those letters and convert them to numbers, each set should use all numbers, each number can occur only once in a set. numbers automatically should increment until 9. and output all possible combinations

PHP
<blockquote class="quote"><div class="op">Quote:</div>function pc_permute($items, $perms = array( )) {
    
    if (empty($items)) { 
      // var_dump(join($perms));
        echo join($perms) . "\n";
       // var_dump(join($perms));
    }  else {
       
        for ($i = count($items) - 1; $i >= 0; --$i) {             
             $newitems = $items;
             var_dump($newitems);
             $newperms = $perms;
             var_dump($newperms);
             list($foo) = array_splice($newitems, $i, 1);            
             array_unshift($newperms, $foo);
             pc_permute($newitems,$newperms);//, $newperms);
         }
    }
}
pc_permute(array('1','2','3')); </blockquote>


Outup is
Quote:

123
213
132
312
231
321



I need to get all combinations plus incrementng till 9, for example if output is '333abc'

output would be
333012
333052
333654
333162
333458
and so on.. untill 789 because need to check for letters not to repeat.

What I have tried:

pc_permute($items, $perms = array( )) {

if (empty($items)) {
// var_dump(join($perms));
echo join($perms) . "\n";
// var_dump(join($perms));
} else {

for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
var_dump($newitems);
$newperms = $perms;
var_dump($newperms);
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems,$newperms);//, $newperms);
}
}
}
pc_permute(array('1','2','3'));
Posted
Updated 14-Apr-16 23:51pm
v3
Comments
Philippe Mori 15-Apr-16 12:25pm    
It is hard to understand what you exactly want because your sample show arbitrary numbers. Why would 333654 be the third number and this is not what you want then why it is your example. If you don't need any particular ordering, then why not having numbers in increasing numbers.
Also, your numbers already start with repeating numbers. In that case, should only numbers identified by a letter are concerned. If so what to do if a letter is duplicated like 1122333aabcb? You have to clearly define your problem. Your specifications are not clear at all. You don't even deserve 1 point...
Philippe Mori 15-Apr-16 12:27pm    
Even worst, you have not even make the effort to use code blocks for all code and properly indent code.

1 solution

I am not sure if this is what you need.

But have a look on this problem:

[^]

and if this is the problem you are facing

solution is here:

[^]

it is in c#
but general alghoritm is described here:
Steinhaus Johnson Trotter permutation algorithm explained and implemented in Java | Tropenhitze[^]
 
Share this answer
 
Comments
Member 12460800 15-Apr-16 5:32am    
this is probably what I want , but I dont need to get permutiations, I neeed to increment digits adn get all combinations so if its '333abc', it doesnt need to output

1 2 3
1 3 2
3 1 2
3 2 1
2 3 1
2 1 3

just permutioate with 123,,, I need to implement and also get combinations until 9, so for example

1 3 2
3 1 4
3 6 1
7 3 1
6 1 8
until
789
Member 12460800 15-Apr-16 5:51am    
this is probably what I want, I not just need to get permutiation of numbers, I need to increment +1 until 9 , for example if '333abc'
output would be

333012
333120
333013
333489
333457
until 333789

your link provides only in 012 and not incrementing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900