Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

hi ... if i've got a set of numbers say "123", how the shortest formula to

resulting the permutation of them to be : "123","132","213","231","312","321"? the result could be flexible depends on the length of numbers, right?

resulting the permutation of them to be : "123","132","213","231","3

if n is the number of characters in the input string, then n! is the number of possible combinations.

--johan

thanks to igor.

I will try to explain my logic. It's very easy;)

To make all combination from the string you need to iterate through all characters in string.

.....

for I:=1 to length(aWork) do

.....

then you need to extract (delete) current character and iterate through remain of the string.

.....

result:=aWork; // I use "result" as undeclared variable

delete(result,I,1); // result keep remain of the string

GenVariant(aBase+aWork[I],

// first parameter keep characters extracted in previous iterations

.....

That's all. It more easy to understand under debugger watching on aBase and aWork parameters.

-----

Igor

PS: if you need not recursive logic then let me know.

--johan

there is another one combinator (not recursive and standalone):

type

TStrCombintor = class(TObject)

fCNT : array of integer;

fIDX : integer;

procedure Init(const V : string);

procedure RollData(var V : string; Index : integer);

function NextCombination(var V : string) : boolean;

end;

......

procedure TStrCombintor.Init(const V : string);

begin

SetLength(fCNT,length(V));

fIDX:=0;

end;

procedure TStrCombintor.RollData(var

var I : integer;

C : char;

begin

C:=V[1];

for I:=1 to Index+1 do V[I]:=V[I+1];

V[Index+1]:=C;

end;

function TStrCombintor.NextCombinat

begin

while fCNT[fIDX]>=fIDX+1 do

begin

RollData(V,fIDX+1);

fCNT[fIDX]:=0;

inc(fIDX);

end;

if fIDX < length(V)-1 then

begin

RollData(V,fIDX+1);

inc(fCNT[fIDX]);

fIDX:=0;

result:=true;

end else result:=false;

end;

---------------

and example how to use it:

procedure TForm1.SpeedButton1Click(S

var SC : TStrCombintor;

S : string;

begin

S := '1234';

SC := TStrCombintor.Create;

SC.Init(S);

repeat

ListBox1.Items.Add(S);

until not SC.NextCombination(S);

SC.Free;

end;

-----

Igor

and another one (not recursive):

-----

procedure MutateStr(S : string);

var C : array of integer;

I : integer;

begin

SetLength(C,Length(S)-1);

I:=0;

repeat

insert(S[1],S,I+3);

delete(S,1,1);

inc(C[I]);

if C[I]=I+2 then

begin

C[I]:=0;

inc(I);

end else

begin

I:=0;

Form1.LB.Items.Add(S)

end;

until I = Length(S)-1;

end;

-----

It doesn't show initial combination, only all another. Do not pass string with length < 2;)

-----

Igor.

var C : array of integer;

i suppose you didn't put the index of array?

this code for D5 you need to change

------OLD----- (D5)

var C : array of integer;

I : integer;

begin

SetLength(C,Length(S)-1);

----NEW---- (D3)

var C : array [0..255] of integer;

I : integer;

begin

FillChar(C,SizeOf(C),#0);

------------

enjoy;)

-----

Igor

--j exposing his thoughts...

;-)

Igor

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

the working sample is bellow:

type

TForm1 = class(TForm)

LB: TListBox;

SpeedButton1: TSpeedButton;

procedure SpeedButton1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.DFM}

function GenVariant(aBase,aWork : string) : string;

var I : integer;

begin

if aWork = '' then Form1.LB.Items.Add(aBase) else

for I:=1 to length(aWork) do

begin

result:=aWork;

delete(result,I,1);

GenVariant(aBase+aWork[I],

end;

end;

procedure TForm1.SpeedButton1Click(S

begin

GenVariant('','123');

end;

-----

Igor.