Combination Algorithm

R

resueman

Can somebody help me with this problem:
I have an array of string

dim nSet as Variant
nSet = Array("A", "B", "C", "D")

I want the function to return all the possible
combinations (n items choose k). Then function, getComb
should return:

("AB","AC","AD","BC","BC","CD") when I use getComb(2, nSet)

And

("ABC","ABD","ACD","BCD") when I use getComb(3,nSet)

And

("ABCD") when I use getComb(4,nSet)

Any help is appreciated!
 
M

Michel Walsh

Hi,

If the values are one character long, in a table, one per record, single
field called f1, no duplicated values, then


SELECT a.f1 & b.f1 & c.f1 & d.f1 As AllMonotoneCombi
FROM ((( MyTable As a LEFT JOIN MyTable As b ON a.f1 >b.f1)
LEFT JOIN myTable As c ON b.f1>c.f1)
LEFT JOIN myTable As d ON c.f1>d.f1


If the values are possibly more than one character long, and we can use a
coma to delimit them:

SELECT a.f1 & ", " & b.f1 & ", " & c.f1 & ", " & d.f1 As AllMonotoneCombi
FROM ...



Hoping it may help,
Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top