Results 1 to 10 of 10

Thread: Combinations of letters

  1. #1

    Thread Starter
    Addicted Member Cbomb's Avatar
    Join Date
    Jul 1999
    Posts
    153

    Exclamation

    I'm pretty sure this is called permutation. WHat I need to do is take a variable number of letters/numbers an compute all the possible combinations. Example:
    Given string: abc
    Results:
    abc
    acb
    bca
    bac
    cab
    cba

    And I have no clue as to how to do this. I need to be able to do this with any number of letters up to 10 at least. Code would be great. But if you could explain the process that would be good too.

    Thanks in advance.
    Cbomb
    Techie

  2. #2
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I dont have the time to write it but this is how it works... you break up every letter in the word into a seperate string...then you randmoize putting the string togehter in diffrent ways and then putting all of that into a list box, or what ever you need... If I had lots of time on my hands I would give you the code. and If i did then it will be like 100 pages long becuase II dont know how to make it short
    NXSupport - Your one-stop source for computer help

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    This is like the nPr thing you see on your scientific calculator. I'm not sure what the formula is off the top of my head, I'll look it up a sec (assuming someone doesn't answer it already).
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    P(n,k) = n·(n-1) ···(n-k+1) = n!/k!

    wheere P(n,k) is the number of permutations (a function of n & k), n is the number of different possible values (3 if you're using the set {a, b, c} for example), and k is the length of your string.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Oh yeah I guess I'll give you some helping using the formula too Basically I'd say write yourself a recursive function that will calculate factorials for you, then just use P = n!/k!.

    Example of code to go in a recursive function to find a factorial:

    Code:
    Public Function Factorial(x as integer)
    
    If x = 1 then
    	Factorial = 1
    Else
    	Factorial = x * Factorial(x-1)
    End If
    
    End Function
    Hmm, it looks simple... too simple... think I may have fogotten something See if it works anyway. And try not to use it with too high an x value, because the function headers will really add up. Well obviously
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Guest
    Try this:

    Code:
    'Code from Megatron
    For I = 0 To 25
        fChar = 97 + I
        For x = 0 To 25
            sChar = 97 + x
            For y = 0 To 25
                List1.AddItem Chr(fChar) & Chr(sChar) & Chr(97 + y)
            Next y
        Next x
    Next I

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    D'oh! I'm not just racking up my post count here, honest Looking at that formula something's wrong with it, here's the right one:

    nPr = n!/(n-r)!

    If n = r then nPr = n!.

    Sorry, wrong letters so I edited it.

    [Edited by HarryW on 09-07-2000 at 10:10 PM]
    Harry.

    "From one thing, know ten thousand things."

  8. #8

    Thread Starter
    Addicted Member Cbomb's Avatar
    Join Date
    Jul 1999
    Posts
    153

    Arrow

    Ok I now can get the number of combinations. But how do I get the combinations themselves? If you told me in that math stuff do forgive me I'm not great at math.
    Cbomb
    Techie

  9. #9
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Do exchanges of first letter.

    The number of permutations goes up fast: 8 Letters (40,320), 10 (3,628,800), 12 (479,001,600).

    The best way to generate them can be done recursively. The general idea is to move the first letter in the string right and left (ABCDEFG, BACDEFG. BCADEFG, BCDAEFG). When first letter gets to one end or the other, advance second letter once before reversing direction of first letter (BCDEFGA, CBDEFGA, CBDEFAG, CBDEAFG). Similarly, when second letter gets to an extreme position, advance third letter.

    The advantage of this method, is that the first letter is moved a huge number of times. It is kept in a temporary location and inserted into the string as required. Thus, most of the exchanges are "half-exchanges" instead of "full exchanges."

    I created a simple VB application which does the above to illustrate the algorithm for a few bright teenage friends. I can Zip the VB files I used and send them to you if you want.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  10. #10

    Thread Starter
    Addicted Member Cbomb's Avatar
    Join Date
    Jul 1999
    Posts
    153
    Oh ok I get it now. If you could send that it would be great! [email protected]

    Thanks!
    Cbomb
    Techie

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