Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non repeating combinations using recursion

Status
Not open for further replies.

Suhmer

Programmer
Nov 16, 2007
2
US
I'm trying to find all non repeating combinations of a set of letters. I'd like to use recursion but can't figure out how to change my code snippet to achieve this. Any ideas would be appreciated. Thanks.

Dim Output(10)
letset = "ABCDE"
Limit = Len(letset)
Counter = 1

For a=1 To Limit
For b=(Counter+1) To Limit
For c=(b+1) To Limit

Output(1) = Mid(letset, Counter, 1)
Output(2) = Mid(letset, b, 1)
Output(3) = Mid(letset, c, 1)

Wscript.Echo Join(Output)

Next
Next

Counter = Counter + 1


-----------------
A B C
A B D
A B E
A C D
A C E
A D E
B C D
B C E
B D E
C D E
 
Code:
letset = array ("A ", "B ", "C ", "D ", "E ")
combi 1, 3, -1, ""
WScript.Quit

sub combi (depth, depthmax, ix, disp)
   dim ii, nextdisp
   for ii = ix + 1 to ubound(letset)
      nextdisp = disp & letset(ii)
      if depth <> depthmax then
         combi depth + 1, depthmax, ii, nextdisp
      else
         WScript.echo nextdisp
      end if
   next
end sub
 
This solution is exactly what I need. This recursive function is elegant and simple [there's a difference between simple and easy]. Thanks xwb for the answer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top