_TITLE "Generate all the combination subsets of an n element set" ' b+ 2020-09-08 DIM set$
, N
, j
, i
, idx
, L$
, k
, tot
, test$
' we will use letters to represent each element of the set
set$ = "abcdefg" ' for starters try a 7 element set
'should be 2^n sets of combinations
DIM subsets$
(0 TO N
), Counts
(0 TO N
)
'designate the empty set as *
subsets$(0) = "*"
Counts(0) = 1
' the single element sets
subsets$
(1) = MID$(set$
, j
, 1) subsets$
(1) = subsets$
(1) + "," + MID$(set$
, j
, 1)Counts(1) = N
FOR i
= 2 TO N
' number of elements idx = 0
Split subsets$(i - 1), ",", iM$()
'try to add the jth element to the i-1 subset to make a subset of i elements
IF INSTR(iM$
(k
), L$
) = 0 THEN ' make a new set of i elements test$ = sorted$(iM$(k), L$)
idx = idx + 1
subsets$(i) = test$
subsets$(i) = subsets$(i) + "," + test$
Counts(i) = idx
tot = tot + Counts(i)
PRINT Counts
(i
);
": "; subsets$
(i
) PRINT tot;
" total subsets when N ="; N
sorted$
= MID$(sort$
, 1, i
- 1) + L$
+ MID$(sort$
, i
)
'notes: REDIM the array(0) to be loaded before calling Split '<<<< IMPORTANT dynamic array and empty, can use any lbound though
'This SUB will take a given N delimited string, and delimiter$ and create an array of N+1 strings using the LBOUND of the given dynamic array to load.
'notes: the loadMeArray() needs to be dynamic string array and will not change the LBOUND of the array it is given. rev 2019-08-27
curpos
= 1: arrpos
= LBOUND(loadMeArray
): LD
= LEN(delim
) dpos
= INSTR(curpos
, SplitMeString
, delim
) loadMeArray
(arrpos
) = MID$(SplitMeString
, curpos
, dpos
- curpos
) arrpos = arrpos + 1
curpos = dpos + LD
dpos
= INSTR(curpos
, SplitMeString
, delim
) loadMeArray
(arrpos
) = MID$(SplitMeString
, curpos
)