Results 1 to 18 of 18

Thread: [RESOLVED] Permutations

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Resolved [RESOLVED] Permutations

    Alright, I have text1 and list1.... now, I want list1 to display all the permutations of the characters in text1, like if text1 had abcd then list1 would show a ab ac ad abc abd abcd... etc...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  2. #2
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Permutations

    Correct me if I am wrong, but when using permutations don't you have to keep the same amount of letters?

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Permutations

    as in? I dont understand... I am not sure if permutations is the right word, but I mean what I said...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  4. #4
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Permutations

    hmm well the only way I could think of is use a mid statement to insert a specific character between each character. Then use split to come up with every possible combination. Let me try to whip up an example function.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Permutations

    Is there a way to make the delimeter character nothing? like make it so the character you have to seperate them with is nothing?
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  6. #6
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Permutations

    Yup, I realized you don't actually need the split.

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Permutations

    There is a whole thread related to this topic with code and everything http://www.vbforums.com/showthread.php?t=463357

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Permutations

    I couldnt find an example...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Permutations

    All the code posted there and you could not find a sample???? There is nothing but samples there...

  10. #10
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Permutations

    You probably did not go to the other pages. Start at the end and work backwards...

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Permutations

    I dont understand much of it, that is why I am asking for help...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  12. #12
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Permutations

    Well, there is working code there. You can step thru the debugger and then ask questions on what you don't understand.

    That is quite different than "can't find a sample..."

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
     
    Posts
    453

    Re: Permutations

    Sorry...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

  14. #14
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Permutations

    Since the number of possible combinations might exceed the limit of the ListBox control, you may want to use something different like a ListView. And optionally lock the window until it has finished loading them to make it faster, ex:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function LockWindowUpdate Lib "user32" Alias "LockWindowUpdate" (ByVal hwndLock As Long) As Long
    4.  
    5. Private Sub Command1_Click()
    6. LockWindowUpdate ListBox1.hWnd
    7.  
    8. 'Permutation code, etc.
    9.  
    10. LockWindowUpdate 0
    11. End Sub

    I think that's right...

  15. #15
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Permutations

    Here is code for review...
    Attached Files Attached Files

  16. #16
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Permutations

    Here is my function for generating permutations of a string. Paste this code in a standard module (*.bas).
    Code:
    Option Explicit
    
    Public Function Permutations(Letters As String, Optional IncludePartial As Boolean = False) As Collection
      Dim colPermutations As Collection
      Dim bytLetters() As Byte
      Dim lngCharCount(255) As Long
      Dim i As Long
      
      Set colPermutations = New Collection
      bytLetters = StrConv(Letters, vbFromUnicode)
      For i = 0 To UBound(bytLetters)
        lngCharCount(bytLetters(i)) = lngCharCount(bytLetters(i)) + 1
      Next i
      Call PermutationRecur(0&, bytLetters, lngCharCount, colPermutations, IncludePartial)
      Set Permutations = colPermutations
      Set colPermutations = Nothing
    End Function
    
    Private Sub PermutationRecur(ByVal Pos As Long, bytLetters() As Byte, lngCharCount() As Long, colPermutations As Collection, IncludePartial As Boolean)
      Dim i As Long
      
      For i = 0 To 255
        If lngCharCount(i) > 0 Then
          lngCharCount(i) = lngCharCount(i) - 1
          bytLetters(Pos) = i
          If Pos = UBound(bytLetters) Then
            colPermutations.Add StrConv(bytLetters, vbUnicode)
          Else
            If IncludePartial Then
              colPermutations.Add Left(StrConv(bytLetters, vbUnicode), Pos + 1)
            End If
            Call PermutationRecur(Pos + 1, bytLetters, lngCharCount, colPermutations, IncludePartial)
          End If
          lngCharCount(i) = lngCharCount(i) + 1
        End If
      Next i
    End Sub
    Paste this code in a form with Command1, Text1, and List1.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Dim colPerms As Collection
      Dim i As Long
      
      Set colPerms = Permutations(Text1.Text, True)
      List1.Clear
      For i = 1 To colPerms.Count
        List1.AddItem colPerms(i)
      Next i
      MsgBox CStr(colPerms.Count) & " permutations"
      Set colPerms = Nothing
    End Sub
    Run the program, type something in the textbox (keep it short...), then click the command button. Presto! Permutations in the listbox!

    Now then, I should tell you that this isn't a very good way to find anagrams. Counting all partial permutations, you'll get over 100,000 from a string of 8 different letters. At best, about 500 of these (~0.5%) will be valid words.

  17. #17
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Permutations

    Here is another:
    Code:
    Private Function GetPermutation(Y As String, Optional X As String = vbNullString) As Long
    Dim idx As Long, pos As Long
    Static cnt As Long
    
        If Len(X) = 0 Then cnt = 0
        pos = Len(Y)
        
        If pos < 2 Then
            mColPermutations.Add X & Y
            cnt = cnt + 1
        Else
            For idx = 1 To pos
                GetPermutation Left$(Y, idx - 1) & Right$(Y, pos - idx), X & Mid$(Y, idx, 1)
            Next
        End If
        GetPermutation = cnt
    End Function
    Example usage
    Code:
    Private mColPermutations As Collection
    
    Private Sub Command1_Click()
    Dim lPerCount As Long, lPermStr As String
    Dim i As Long
    
        lPermStr = "SomeStr"
    
        Set mColPermutations = New Collection
        lPerCount = GetPermutation(lPermStr) '(Recursive Function)
        
        'Show them..
        List1.Clear
        List1.AddItem "Permutations found: " & lPerCount
        For i = 1 To mColPermutations.Count
            List1.AddItem mColPermutations(i)
        Next
        
        Set mColPermutations = Nothing
    End Sub
    Last edited by jcis; Aug 14th, 2007 at 02:40 AM.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Location
    &nbsp;
    Posts
    453

    Re: Permutations

    WoW, thank you all...
    Haikus are easy.
    But sometimes they don't make sense.
    Refrigerator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width