1

How do you find all the combinations of a 40 letter string?

I have to find how many combinations 20 D and 20 R can make.

as in one combination could be...

DDDDDDDDDDDDDDDDDDDDRRRRRRRRRRRRRRRRRRRR

thats 1 combination, now how do I figure out the rest?

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • what is it, !20 ways of arranging Ds and Rs? Isn't this problem similar to the problem of generating all possible sub-sets from a given set? – Aman Oct 02 '12 at 22:14
  • I believe so. There should be 40 ways of doing it... – FabianCook Oct 02 '12 at 22:16
  • 3
    Can you clarify, do you want to actually generate all of combinations or simply know how many unique combinations there are? (One answer involves itertools; the other basic mathematics) – Merbs Oct 02 '12 at 22:18
  • To know how many there are :) – FabianCook Oct 02 '12 at 22:19
  • 1
    This probably belongs on http://math.stackexchange.com/ – ernie Oct 02 '12 at 22:19
  • I want a programming answer though, not all people on math will know any language at all... – FabianCook Oct 02 '12 at 22:20
  • There are 137,846,528,820 ways of arranging the 20 Ds and 20 Rs. – Gareth Rees Oct 02 '12 at 22:21
  • I don't want the direct answer... I want to know how you would go about getting it... – FabianCook Oct 02 '12 at 22:22
  • 1
    So ... are you sure you want combinations and not permutations? – Merbs Oct 02 '12 at 22:22
  • 1
    The reason I suggested math.stackexchange.com is you said you wanted the answer and how to go about getting it. It's much more efficient to do this using math, than building out all the options. Alternatively, research permutations and combinations for the direct formula. – ernie Oct 02 '12 at 22:25
  • If you insist upon a programming approach, try looking at http://docs.python.org/library/itertools.html#itertools.permutations which even states "the number of items returned is n! / (n-r)! when 0 <= r <= n or zero when r > n." – Merbs Oct 02 '12 at 22:27
  • 1
    I have a feeling that some of the answers here will lead you to the wrong answer, because you probably want unique permutations, which isn't what itertools will give you; however you'll find the answer here: http://stackoverflow.com/questions/6284396/permutations-with-unique-values – Merbs Oct 02 '12 at 22:44
  • If you wanted to actually generate the unique permutations (which I don't suggest), one way to do it would be to use `itertools.combinations(range(40), 20)`. Each element returned here would be a tuple of 20 integers, which would be the indices of each `D` in that particular permutation. – Andrew Clark Oct 02 '12 at 22:55
  • Yeah. I figured that took way to much time xD – FabianCook Oct 02 '12 at 23:11

5 Answers5

2

To count every combination of 20 D and 20 R, we can think of there being 40 "slots", 20 of these slots will be filled by D, and the rest will be filled by R. So, we can calculate the total number of combinations using C(40, 20), or 40 choose 20, which can be represented with the following formula:

40!/(20!*(40-20)!)

Or in Python:

>>> import math
>>> math.factorial(40) / (math.factorial(20) * math.factorial(40-20))
137846528820L

Note that this is the same thing as the number of unique permutations of a string with 20 D and 20 R, but if you just calculate the number of permutations of that string you will be counting a lot of duplicates, and if you tried to calculate this by creating each permutation it will take a very long time.

If you wanted to actually generate the unique permutations (which I don't suggest), one way to do it would be to use itertools.combinations(range(40), 20). Each element returned here would be a tuple of 20 integers, which would be the indices of each D in that particular permutation.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
1

There are a lot of permutations. It usually isn't a good idea to generate them all just to count them. You should look for a mathematical formula to solve this, or perhaps use dynamic programming to compute the result.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

Here's a nice function built into itertools:

This taken directly from this link: 9.7. itertools — Functions creating iterators for efficient looping

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

Edit

I've included a link I found on SO which gives a fine example of doing this using JavaScript: Is there any pre-built method for finding all permutations of a given string in JavaScript?

If you don't mind PHP, here's an example taken directly from this link: 4.26. Finding All Permutations of an Array

Note: To use the pc_permute function, you would need to separate your string into a character array.

function pc_permute($items, $perms = array( )) {
    if (empty($items)) { 
        print join(' ', $perms) . "\n";
    }  else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
             list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             pc_permute($newitems, $newperms);
         }
    }
}
Community
  • 1
  • 1
Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • Was your last answer in js? I could have worked with it. – FabianCook Oct 02 '12 at 22:19
  • Actually it was in PHP (I misread). But, if you want JavaScript, [here is a link I found on SO](http://stackoverflow.com/questions/5232295/is-there-any-pre-built-method-for-finding-all-permutations-of-a-given-string-in) that I believe will help. – Jeremy Oct 02 '12 at 22:22
  • I don't really mind what language xD – FabianCook Oct 02 '12 at 22:23
  • Updated answer to include JavaScript reference as well as PHP example. – Jeremy Oct 02 '12 at 22:25
  • would the amount of permutations be to large? – FabianCook Oct 02 '12 at 22:27
  • That depends on your definition of 'too' large. There is an inherent danger of computing permutations as the calculation is essentially exponential. – Jeremy Oct 02 '12 at 22:30
0

Depending on what you want to do with the combinations, there are a lot of ways to figure this out. If you just want to know the number of combinations/permutations that exist, math can tell you that.

If you want to do something like build a brute force algorithm that checks every possible arrangement of a given set of characters, itertools.product() can help.

http://docs.python.org/library/itertools.html#module-itertools

SciurusDoomus
  • 113
  • 2
  • 14
0

Here's a generator which generates all distinct permutations.

def distinct_pem(A,D):
    if A==0: yield 'D'*D
    elif D==0: yield 'A'*A
    elif A==1 and D==1:
        yield 'AD'
        yield 'DA'
    else:
        if A>=2:
            for string in distinct_pem(A-2,D):
                yield  'AA'+string
        if A>1 and D>1:
            for string in distinct_pem(A-1,D-1):
                yield  'AD'+string
                yield  'DA'+string
        if D>=2:
            for string in distinct_pem(A,D-2):
                yield  'DD'+string

For 10 this is very fast, but it struggles with 20 (perhaps it can be made more efficient?).

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535