Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pattern / Combinations

371 views
Skip to first unread message

Paul W Smith

unread,
Mar 17, 2006, 9:19:13 AM3/17/06
to
Does anyone know of any algorithms which produce all the possible
combinations of patterns within a string?

Example String = ABCDE

AB, AC, AD, AE, BC, BD, BE, CD, CE, DE
ABC, ABD, ABE, ACD, ACE, ADE, BCD, BCE, CDE
ABCD, ABCE, ABDE, ACDE, BDCE
ABCDE


Andrew Taylor

unread,
Mar 17, 2006, 9:35:08 AM3/17/06
to
A simple recursive approach seems to work quite well (though
it doesn't give them in alphabetical order):

Option Explicit
Sub main()
ShowCombinations "", "ABCDE"
End Sub

Sub ShowCombinations(strPrefix As String, strMain As String)
If strMain = "" Then
Debug.Print strPrefix
Exit Sub
End If
Dim strFirst As String, strRest As String
strFirst = Left(strMain, 1)
strRest = Mid(strMain, 2)
ShowCombinations strPrefix & strFirst, strRest
ShowCombinations strPrefix, strRest
End Sub

Tom Ogilvy

unread,
Mar 17, 2006, 9:59:29 AM3/17/06
to
Very Nice!

--
Regards,
Tom Ogilvy

Paul W Smith

unread,
Mar 17, 2006, 10:05:03 AM3/17/06
to
Thank you for this example which is so close to what I require - order is
unimportant.

However how do I amend your code so it only produces doubles and above, no
singles

PWS


"Andrew Taylor" <andrew...@cantab.net> wrote in message
news:1142606108....@j33g2000cwa.googlegroups.com...

Paul W Smith

unread,
Mar 17, 2006, 10:23:37 AM3/17/06
to
The answer to my question came to me as soon as I posted my request - check
that the length of StrPrefix > 1.


"Paul W Smith" <pwsN...@twelve.me.uk> wrote in message
news:441ad01d$0$9236$ed26...@ptn-nntp-reader01.plus.net...

Dana DeLouis

unread,
Mar 19, 2006, 3:22:18 AM3/19/06
to
> Very Nice!
I like it also. In other math programs, this is known as "BinarySubsets."
This implementation gives it in reverse order. With 5 items, it goes from
2^5-1 to 1 in Binary form. Other programs use this idea for implementation.
First number is 31, or 11111 in binary, (abcde)
Second number is 30, or 11110 (abcd)
29, 11101 (abce)
etc...
--
Dana DeLouis
Windows XP, Office 2003


"Tom Ogilvy" <TomO...@discussions.microsoft.com> wrote in message
news:418DF7D9-42F1-4294...@microsoft.com...

0 new messages