2

Im trying to write algorithm to get all combinations and increment by one at a same time and get this

function generate($index){
    $xxx = '';
    $flag = 0;  
    $lengtchString = strlen($index); 
    for($y = 0; $y != $lengtchString; $y++) {
        $xxx .= "$y";
    }
    while ($flag != $lengtchString ) 
    { 
        for ($i = 0; $i<$lengtchString-1; $i++) 
        {            
            $temp = $xxx[$i];  
            $xxx[$i] = $xxx[$i+1]; 
            $xxx[$i+1] = $temp;   
            echo $xxx."<br>"; 
        } 
        $flag++; 
    }
}
generate('abc');

and output is

102
120
210
201
021
012

I need to get all combinations not only for 3 digits but also all combinations .

For example if I write 'abc'... I need output like

102
120
210
201
021
012
256
874
569
236
254
028

and so on.... untill 789 in condition that digits wouldnt repeat... my mind actually to be blown, cant get proper algorithm for that. thanks in advance

dhruv jadia
  • 1,684
  • 2
  • 15
  • 28
  • you have used $y != $lengtchString for fetching $xxx data so its never give you output like 256 because length is always 3 and it will give you combination of 0,1,2 in your output. – RJParikh Apr 18 '16 at 06:39
  • I know but I cant mind to get proper code, do you have any other suggestions? – Hracho Avagyan Apr 18 '16 at 06:54
  • Your last example is a bit misleading I am assuming you want all 3 digit numbers where the digits are from `{ 0,1,2,3,4,5,6,7,8,9, }` and each digit is distinct in a number (so `111` is invalid) are there any other constraints or you just miss the few starting combinations ... see [iterative permutations without repetitions in C++](http://stackoverflow.com/a/30498154/2521214) you just need add to your program `flag` if the digit is already used ... – Spektre Apr 18 '16 at 07:28

2 Answers2

0

Check this link PHP algorithm to generate all combinations of a specific size from a single set

    <?php
    function sampling($chars, $size, $combinations = array()) {
    $charsArray = str_split($chars);
    # if it's the first iteration, the first set 
    # of combinations is the same as the set of characters
    if (empty($combinations)) {
        $combinations = $charsArray;
    }

    # we're done if we're at size 1
    if ($size == 1) {
        return $combinations;
    }

    # initialise array to put new values in
    $new_combinations = array();

    # loop through existing combinations and character set to create strings
    foreach ($combinations as $combination) {
        foreach ($charsArray as $char) {
            $new_combinations[] = $combination . $char;
        }
    }
    # call same function again for the next iteration
    return sampling($chars, $size - 1, $new_combinations);

}

// example

$output = sampling("0123456789", 3);
print "<pre>";print_r($output);
Community
  • 1
  • 1
Brijal Savaliya
  • 1,101
  • 9
  • 19
  • man , thanks for response... but this doesnt prints out till 9 if I will write 012, I tried this algorithm but doesnt output what I need – Hracho Avagyan Apr 18 '16 at 07:41
  • this example is for whatever combinations you need in result e.g if you pass "0,1,2" you will get all combinations of "0", "1" and "2" @HrachoAvagyan – Brijal Savaliya Apr 18 '16 at 07:45
  • Thank you so much... and one little question , how to use this if a function argument is only one and its a a string?. i.e. ''012''. not array . – Hracho Avagyan Apr 18 '16 at 10:58
  • @HrachoAvagyan I have updated code please check now we have to pass string e.g. sampling("012", 3); , please let me know this code is useful to you or not ? – Brijal Savaliya Apr 18 '16 at 12:26
  • well works nice, but I want to try out non-recursive method of this. trying to have combinations using only clear iteration. – Hracho Avagyan Apr 18 '16 at 12:44
  • I think without recursive it is not possible ,we have to use recursive for all combinations we want, please fill free to UV for this answer if it is helpful to you :) – Brijal Savaliya Apr 18 '16 at 12:46
  • I think the best way for iteration is to change places of values but its not helpfull to get all combinations untill 9 – Hracho Avagyan Apr 18 '16 at 12:50
  • Thank you @HrachoAvagyan ;) – Brijal Savaliya Apr 18 '16 at 12:50
0

Delphi function generates all combinations of chars from given charset without char repeats (if there are no repeats in source string).

How it works:
Let's at some stage we have

charset = '1203456789'
incomplete result = '12'
i goes from 3 to 10

i = 3:  charset doesn't change; recursive call executes with '123' and StartIndex = 4
i = 4: charset changes to '1204356789'; recursive call executes with '124' and StartIndex = 4; 
       charset changes back to '1203456789'
i = 5: charset changes to '12054356789'; recursive call executes with '125' and StartIndex = 4; 
       charset changes back to '1203456789'

and so on...

procedure GenString(StartIndx, MaxLen: Integer; Charset, Result: string);

procedure Swap(a,b: Integer);
var
  t: Char;
begin
  t := CharSet[a];
  CharSet[a] := CharSet[b];
  CharSet[b] := t;
end;

var
  i: Integer;
begin
  if Length(Result) = MaxLen then
    Memo1.Lines.Add(Result)  // output result
  else begin
    for i := StartIndx to Length(Charset) do begin
      Swap(i, StartIndx);  //save current char to avoid further usage
      GenString(StartIndx + 1, MaxLen, Charset, Result + Charset[StartIndx]);
      Swap(i, StartIndx); //restore order
    end;
  end;
end;

usage: //Delphi strings are 1-based!
  GenString(1, 3, '0123456789', '');

output (720 values):

012
013
014
015
016
017
018
019
021
023
024
025
026
027
028
029
032
031
...
986
987
981
980
902
903
904
905
906
907
908
901
MBo
  • 77,366
  • 5
  • 53
  • 86
  • thank you for response, in which programming lenguage is this written? C++? I cant understand this – Hracho Avagyan Apr 18 '16 at 08:02
  • Thank you, I will try to convert this to php – Hracho Avagyan Apr 18 '16 at 08:37
  • can you help me a little bit. how to converts Memo1.Lines.Add(Result) to php . and also procedure Swap to php. thanks. – Hracho Avagyan Apr 20 '16 at 06:55
  • Memo1.Lines.Add(Result) just outputs Result string to the screen. Probably `echo`? Swap exchanges two symbols in the string at given indexes - like you make swap of $xxx[$i] and $xxx[$i+1] in your code – MBo Apr 20 '16 at 07:00
  • yes its echo, thank you so much:) ... so you are using recursion as I understand – Hracho Avagyan Apr 20 '16 at 07:14
  • this is nice solution but I want something actually different, I want to be done by mask, so if its "333abbcd" output gonna be 33301123 33301124 33301125 33302236 33375569 and so on... – Hracho Avagyan Apr 20 '16 at 12:37
  • But you did not write about matching to mask. This is not a problem - find number of different chars = M, generate M-length combinations and substitute every mask char with corresponding digit of every combination – MBo Apr 20 '16 at 14:43