Z3R0UN1T
01-07-2008, 01:25 PM
I have nice combination generator but there is one disadvantage:
There is no possibility to choose minimal length of combination.
You can choose maximal length of combination but not minimal. :(
I have one idea to use this:
If LogToFile And STRblah > txtMiniLen.txt Then
Print #1, STRblah
End If
But this is bad solution because need wait while generator generate all combinations > txtMiniLen.txt :(
So maybe someone can help me and show me how to add "minimal length of combination" parameter to this combination generator. :confused:
27769
mkaras
01-08-2008, 06:13 AM
You would really have to help us help you. There are certain things that would go a long way toward that goal.
1) If you want to post code that someone is expected to look at you should explain what it is supposed to be doing.
2) To understand your code it really needs to be formatted in a way that others will find it easier to read.
3) Many people that see uncommented code will just walk away from it so you really should comment the code in some reasonable manner.
4) You would do a lot better to extract the part of the code that you are having trouble with and place it right in the posting so that you can get answers and support without people having to download, save and open your project files.
Now all that said I'll make a point about your comment:
Z3R0UN1T:
There is no possibility to choose minimal length of combination.
This is puzzling because the algorithm(s) in your program are self contained and you as the programmer have full control over how they function. Seems to me you need to look at how to get your code to simply not do strings shorter than you need instead of working through all those that you want to end up discarding. i.e. Why take bites out of all the white bread sandwiches when you know ahead of time you are only interested in the dark rye bread ones.
Z3R0UN1T
01-08-2008, 08:03 AM
Thanks for reply mkaras, glad that someone made attention for my request. :)
Small description:
This code can generate all possible combinations of given characters and save them to *.TXT file.
Part of core code from "Combination Generator.zip":
Option Explicit
Dim Total As Double
Dim Completed As Double
Dim LastCompleted As Long
Dim LogToFile As Boolean
Dim PrintOutput As Boolean
Dim STRChars As String
Dim MapChars() As String * 1
Dim LenSTRChars As Long
Private Sub Command1_Click()
Dim X As Integer
Command1.Enabled = False
Total = 0
Completed = 0
Timer1.Enabled = True
If Check1.Value Then
Open txtSave.Text For Output As #1
LogToFile = True
Else
LogToFile = False
End If
Check1.Enabled = False
For X = 0 To txtLen - 1
Total = Total + (Len(txtString) ^ (txtLen - X))
Next
Label1.Caption = "Completed: " & 0
Label2.Caption = "Possible: " & Total
STRChars = txtString.Text
ReDim MapChars(Len(STRChars)) As String * 1
For X = 1 To Len(STRChars)
MapChars(X) = Mid(STRChars, X, 1)
Next
LenSTRChars = Len(STRChars)
Combinations txtStart.Text, txtLen
Timer1.Enabled = False
Label1.Caption = "Completed: " & Completed
If Check1.Value Then
Close 1
End If
Check1.Enabled = True
Command1.Enabled = True
End Sub
Private Function Combinations(ByVal X As String, ByVal Y As Byte)
Dim P As Byte
Dim STRblah As String
For P = 1 To LenSTRChars
STRblah = X & MapChars(P)
If LogToFile Then
Print #1, STRblah
End If
If Not Len(STRblah) = Y Then
Combinations STRblah, txtLen
End If
Completed = Completed + 1
Next
DoEvents
End Function
Private Sub Timer1_Timer()
Label1.Caption = "Completed: " & Completed
Label2.Caption = "Possible: " & Total
LastCompleted = Completed
End Sub
Problem:
There is possibility to choose maximal length of combination. <--- Good
But there is no possibility to choose minimal length of combination. <--- Bad (disadvantage)
Request:
Maybe someone can help me and show me how to add "minimal length of combination" parameter to this code. :-\
P.S
I really appreciate your help and sorry my English grammar isn't so good. :o
Robert Collins
01-08-2008, 11:27 AM
This code can generate all possible combinations of given characters and save them to *.TXT file.
So if given characters are ABC then all possible combinations are:
ABC
ACB
BAC
BCA
CAB
CBA
Is this correct?
There is possibility to choose maximal length of combination.
But there is no possibility to choose minimal length of combination
Here's where I'm confused.
Maximal length of above example combinations is 3. Is this correct?
So what do you mean by minimal length? Can you give an exact example?
If what I posted is completely off base then just ignore this post.
Z3R0UN1T
01-08-2008, 11:53 AM
For example given characters are AB and maximal length of combination is 3 then we will get this:
A
AA
AAA
AAB
AB
ABA
ABB
B
BA
BAA
BAB
BB
BBA
BBB
So minimal lenght of combination is 1 and maximal is 3 !
For example how can I generate all combinations with lengh from 2 to 3 ?:
AA
AAA
AAB
AB
ABA
ABB
BA
BAA
BAB
BB
BBA
BBB
So if given characters are ABC then all possible combinations are:
ABC
ACB
BAC
BCA
CAB
CBA
Is this correct?
Robert seems to be confused about the difference between permutations and combinations.
Robert Collins
01-08-2008, 12:06 PM
Oh, I see. You set the maximum length.
Can you not just allow it to generate the maximum length then go through the output and extract out only the ones you want by desired lengths
For i = 0 To TotalNumberOfPossibilities
If Len(OutputCombination[i]) >= 2 Then
'
' Output this combination to the output text file
'
End If
Next i
...or is this unacceptable?
EDIT:
OK, never mind. I can see this wouldn't be acceptable
Z3R0UN1T
01-08-2008, 01:22 PM
Oh, I see. You set the maximum length.
Can you not just allow it to generate the maximum length then go through the output and extract out only the ones you want by desired lengths
For i = 0 To TotalNumberOfPossibilities
If Len(OutputCombination[i]) >= 2 Then
'
' Output this combination to the output text file
'
End If
Next i
...or is this unacceptable?
EDIT:
OK, never mind. I can see this wouldn't be acceptable
Some time ago I was thinking about that to, but this idea is not so good because if minimal lenght is for examle 4 or 5 and we have given big amount of various characters then we have wait some minutes until we get an result. So I think that this is not solution.
Robert Collins
01-08-2008, 01:51 PM
Hence the EDIT statement:D
Now that I think about it I was never confused about the difference between permutations and combinations.:p
Robert Collins
01-08-2008, 02:04 PM
OK, then can you not just check for a minimal length within the execution of the function as each combination is made rather than to check for all of them at the end? I'm just not sure where to place it in that function.
Robert Collins
01-08-2008, 05:05 PM
I tried running your code as listed but am having problem with a item or two.
I assume that txtString is for the combination text like AB, and
txtLen is for the minimal value like you want 2 to 3 I enter 2
But I don't know what txtStart is.
If I could run this code I can better find a way to solve your problem.
Just out of curriosty why do you need a timer?
Roger_Wgnr
01-08-2008, 08:20 PM
First way to stop printng of combinations less than Minlength
If LogToFile And LenB(STRblag) > Minlength Then
Print #1, STRblah
End If
Second way
For P = 1 To LenSTRChars
If LenB(x) < Minlength then
x = String(Minlength, x)
End if
STRblah = X & MapChars(P)
That is if I understand your code without any comments it's hard to follow.
You may need to make it x = String(Minlength - 1, x)this should give you a little direction anyway. I would approach it using the second method as the first will still generate all of the unused combinations where the second method will not even create them.