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?
To count every combination of 20
Dand 20R, we can think of there being 40 “slots”, 20 of these slots will be filled byD, and the rest will be filled byR. 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:Or in Python:
Note that this is the same thing as the number of unique permutations of a string with 20
Dand 20R, 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 eachDin that particular permutation.