?
Solved

Logic Problem

Posted on 2000-05-18
22
Medium Priority
?
148 Views
Last Modified: 2010-04-04
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?


0
Comment
Question by:powerfool
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
  • +4
22 Comments
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
ID: 2820679
Hi,
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],result);
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  GenVariant('','123');
end;


-----
Igor.
0
 

Expert Comment

by:Grayl1
ID: 2821531
Hi!

Do you want just one random combination or all of them?
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2821685
Hey Igor, nice code :-)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Expert Comment

by:sageryd
ID: 2825012
Nice code Igor, but how does it really work?
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2825015
Just a tip:

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


--johan
0
 
LVL 1

Author Comment

by:powerfool
ID: 2825213
that's a good logic, is there anyone could give other alternative? I will give my point in few next days ok ...
thanks to igor.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2828641
Hi all,
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],result); // recursive call to iterate with remain of the string
    // 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.
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2828649
Well....I can't exactly say I understood more of it when you explained but I guess I'll understand it when I debug it in Delphi. Great code though! Good work, Igor!

--johan
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2828755
Hi all,
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 V : string; Index : integer);
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.NextCombination(var V : string) : boolean;
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(Sender: TObject);
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
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2832548
Hi,

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.
0
 
LVL 3

Expert Comment

by:wuyinzhi
ID: 2843963
hi igor, i tried your code above with Delphi 3 and found error at this line:

var C : array of integer;

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

0
 
LVL 9

Expert Comment

by:ITugay
ID: 2844008
hi wuyinzhi,

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
0
 

Expert Comment

by:JohnnyBoy
ID: 2853917
Isn't it


counter = length of data
for iLoop = 1 to counter
  answer = answer * counter
0
 
LVL 1

Author Comment

by:powerfool
ID: 2855366
hi JohnnyBoy, I don't think your "answer" is an answer for the question, please refer to ITugay's program, he's got best solution so far.

0
 
LVL 1

Expert Comment

by:sageryd
ID: 2855396
Why don't we end this thread? Igor has come up with many different variants of the code, propose an answer and end the thread, Igor!

--j exposing his thoughts...
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2857716
I wouldn't like to propose an answer. May be another experts have interest solutions. And it's decigion of questioner to accept comment as answer.
;-)

Igor
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2858504
Ok! I was just, as I said, exposing my current thought! ;o)

johan
0
 
LVL 1

Author Comment

by:powerfool
ID: 2863039
i already accepted igor's comment as right answer and gave my points. how could I do to lock this thread?
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2863914
No you haven't!
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2866553
hi powerfool,
If you like my answers then just click on "accept comment as answer" ;)
Igor.
0
 
LVL 1

Author Comment

by:powerfool
ID: 2879708
Comment accepted as answer
0
 
LVL 1

Author Comment

by:powerfool
ID: 2879709
Ok. I do it now.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question