Discussion:
Getting all versions (array of array)
(too old to reply)
jodleren
2010-11-14 19:34:55 UTC
Permalink
Hi all

I dont how to call it so:

Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
options:

1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5

What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?

Sonnich
Jamie
2010-11-14 21:18:33 UTC
Permalink
Post by jodleren
Hi all
Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
Sonnich
to do that..
Number of entries:= 6^8 = 1679616; that is over 1.5Megs of memory
if you use integer numbers..

And if you are using arrays to hold each number of 4's. This is
times that
so

Total_Memory := 1679616*4=6718464; almost 7Megs of memory..
Using bytes will save the day instead of integers, because
that would me 4*7M= 28Megs..

Var
MyArray:Array of array of byte.. ; I think that is the correct way?

Begin
Setlength(Myarray,1679616,4);

///....
Myarray[1,0] := ?;


End;

Remember that dynamic arrays start at 0 index not 1..


hope that helped a little...

P.S.
You can redeclare the size of the array with out losing what is
already in it, unless you go smaller ofcoures, and in this case only
the first element should be resized. This shouldn't pose as a problem
for you.
jodleren
2010-11-14 22:10:39 UTC
Permalink
On Nov 14, 11:18 pm, Jamie
Post by Jamie
Post by jodleren
Hi all
Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
Sonnich
to do that..
   Number of entries:= 6^8 = 1679616; that is over 1.5Megs of memory
if you use integer numbers..
I am not so sure - it might be if I want random order, but I do not
want that. I want the options with the numbers in. Basically my list
should be with 6 in the end - just that I realised that the moving 6
works in that case.

You could see it as: (w/5 numbers)

5*4*3*2
But I only allow a higher number after the present one, this limits is
quite a but.

but, if we start with 2 (second no) it becomes:
1*3*2*1 (there is only one option - 2-4-5-6)
3rd*
- none
starting with one:
1*4*3*2 (4 options, I wonder is that is the last 2 squared?)

so, w/7 numbers
1*6*5*4 = 4^2 = 16
2*5*4*3 = 3^2 = 9
3*3*2*1 = 1^2 = 1
total 26
I guess less, that 5*4 will be less in some cases.
Post by Jamie
   And if you are using arrays to hold each number of 4's. This is
times that
  so
Total_Memory := 1679616*4=6718464; almost 7Megs of memory..
Using bytes will save the day instead of integers, because
that would me 4*7M= 28Megs..
Var
   MyArray:Array of array of byte.. ; I think that is the correct way?
Begin
   Setlength(Myarray,1679616,4);
   ///....
   Myarray[1,0] := ?;
End;
  Remember that dynamic arrays start at 0 index not 1..
  hope that helped a little...
  P.S.
    You can redeclare the size of the array with out losing what is
already in it, unless you go smaller ofcoures, and in this case only
the first element should be resized. This shouldn't pose as a problem
for you.
Ph. B.
2010-11-14 22:35:58 UTC
Permalink
Post by jodleren
Hi all
Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
Sonnich
Hi Sonnich,

This is a combinatorial problem...
I think it depends on the correct assumption. What exactly do you need ?

It could be "Counts", "Arrangements", "Combinations"...

IMHO, in your case, i think it is a "Combinations without repetition"

C(n,k) = n! / (k! * (n - k)!)
C(5,4) = 5! / (4! * (5 - 4)!) = 120 / (24 * 1) = 5 combinations

1,2,4,5
1,2,4,6
1,2,5,6
1,4,5,6
2,4,5,6

This is for theory.

Typically, you can solve this with 4 nested loops as follows :
(a form with a TMemo and a TButton)

procedure TForm1.Button1Click(Sender: TObject);
const
Values : array[1..5] of integer = (1, 2, 4, 5, 6);
var
i, j, k, l : integer;
begin
Memo1.Clear;
Memo1.Lines.Add('Combinations C(5,4) for values : 1, 2, 4, 5, 6');
Memo1.Lines.Add('');

for i := 1 to 2 do // 2 = n - k + (k - 3)
for j := i + 1 to 3 do // 3 = n - k + (k - 2)
for k := j + 1 to 4 do // 4 = n - k + (k - 1)
for l := k + 1 to 5 do // 5 = n - k + k
Memo1.Lines.Add(
IntToStr(Values[i]) + ', ' +
IntToStr(Values[j]) + ', ' +
IntToStr(Values[k]) + ', ' +
IntToStr(Values[l]));
end;

Finally, you have to adapt this small example to yours needs...

Regards,
Philippe
jodleren
2010-11-15 18:37:28 UTC
Permalink
On Nov 15, 12:35 am, "Ph. B."
Post by Ph. B.
Post by jodleren
Hi all
Say, I have some numbers, 1,2,4,5,6 and I need all possible 4-number
1,2,4,5
1,2,4,6
1,2,6,5
1,6,4,5
6,2,4,5
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
Sonnich
Hi Sonnich,
This is a combinatorial problem...
I think it depends on the correct assumption. What exactly do you need ?
It could be "Counts", "Arrangements", "Combinations"...
IMHO, in your case, i think it is a "Combinations without repetition"
C(n,k) = n! / (k! * (n - k)!)
C(5,4) = 5! / (4! * (5 - 4)!) = 120 / (24 * 1) = 5 combinations
1,2,4,5
1,2,4,6
1,2,5,6
1,4,5,6
2,4,5,6
This is for theory.
(a form with a TMemo and a TButton)
procedure TForm1.Button1Click(Sender: TObject);
const
   Values : array[1..5] of integer = (1, 2, 4, 5, 6);
var
   i, j, k, l : integer;
begin
   Memo1.Clear;
   Memo1.Lines.Add('Combinations C(5,4) for values : 1, 2, 4, 5, 6');
   Memo1.Lines.Add('');
   for i := 1 to 2 do            // 2 = n - k + (k - 3)
     for j := i + 1 to 3 do      // 3 = n - k + (k - 2)
       for k := j + 1 to 4 do    // 4 = n - k + (k - 1)
         for l := k + 1 to 5 do  // 5 = n - k + k
           Memo1.Lines.Add(
             IntToStr(Values[i]) + ', ' +
             IntToStr(Values[j]) + ', ' +
             IntToStr(Values[k]) + ', ' +
             IntToStr(Values[l]));
end;
Finally, you have to adapt this small example to yours needs...
Regards,
Philippe
Thanks

I came up with this, and it seems to work:

Given:
RowCount - numbers in row - here 4
FTotal - numbers available - here 5
FNumbers - then numbers in use.


var
Counters: array of integer;
i, j, iCounter, k: integer;
sTemp: string;
bGo: boolean;
begin
try
SetLength(Counters, RowCount);

for i := 0 to RowCount-1 do
Counters[i] := i;

k := 0;
while Counters[0] < (FTotal - RowCount + 1) do
begin
sTemp := '';
for i := 0 to RowCount-1 do
sTemp := sTemp + IntToStr(FNumbers[Counters[i]]) + ' ';
Memo1.Lines.Add(sTemp);

iCounter := RowCount - 1;
repeat
if Counters[iCounter] < (FTotal-(RowCount-iCounter)) then
begin
Inc(Counters[iCounter]);
for j := iCounter +1 to RowCount-1 do
Counters[j] := Counters[j - 1] + 1;
bGo := False;
end
else
begin
Dec(iCounter);
bGo := (iCounter > -1);
if iCounter = -1 then // stop it all
Counters[0] := FTotal + 1;
end;
until not bGo;
Inc(k); // count items generated
end;

Edit1.Text := IntToStr(k);
except
end;
end;
Dr J R Stockton
2010-11-15 20:14:23 UTC
Permalink
In comp.lang.pascal.delphi.misc message <fef15d69-9745-4861-a39b-
Post by jodleren
What I see is the 6 move - by say I have more numbers? Is there a way
to get them all? Say, I have 8 numbers and what to get all 4 number
options?
That problem reduces to selecting one number from the eight, followed by
selecting one from those of the remaining seven which are larger than
it, followed ..., followed ... .

If the numbers are not all different, put them in a list, sort it, and
replace "larger" by "later in the list".

That implements as a simple recursive routine which if run will output
all required possibilities in logical order.

If you also require the results in different orders internally, then
replace "larger" with "not already used".

In writing your routine, be careful EITHER to use call-by-copying, or
surround the internal calls by "remove just-chosen element" and "replace
it".

It may help if you start with eight playing cards and implement the
intended algorithm manually so that you can see what you are doing; or
if you add sufficient print statements to the code. Test first with
1-from-3, 2-from-4, etc.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Loading...