isn't this a common compsci hw problem?

new topic     » goto parent     » topic index » view thread      » older message » newer message
cp said...

If I have something like

for a = 1 to length(sMySeq[1]) do 
    for b = 1 to length(sMySeq[2]) do 
        for c = 1 to length(sMySeq[3]) do 
        .... 

It seems this will only handle the 3 sequences in sMySeq. What if sMySeq now looks like

sMySeq = {{a1,a2},{b1,b2},{c1,c2},{d1,d2,d3}} 

I'm thinking this needs some sort of recursive function but just not sure how to go about it. thanks

Hmm, I assumed you could just iterate it through with two for loops, but now I realize it's a bit more complicated than that. You'd need something like this:

sequence sCombo, sCombo_old 
sCombo = {} 
sCombo_old = sMySeq[1] 
for i = 2 to length(sMySeq) do 
	for k = 1 to length(sCombo_old) do 
		for j = 1 to length(sMySeq[i]) do 
			sCombo = append(sCombo, sCombo_old[k] & sMySeq[i][j]) 
		end for 
	end for 
	sCombo_old = sCombo 
	sCombo = {} 
end for 

Which does strongly suggest that this problem is naturally suited to be solved using a recursive function, for obvious reasons.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu