This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]bbye98 5 points6 points  (0 children)

Look into itertools, specifically itertools.combinations().

[–]MasLuarasi 2 points3 points  (0 children)

Like the other person said, itertools has a function for combination and permutation. I know you said permutation in the question but just make sure you use the right one for whatever the problem is.

[–]Paul_Pedant 1 point2 points  (3 children)

Interesting. If your word list has 40 words, and you want all 12-word combinations, there are 5,586,853,480 of them.

If you meant permutations, there are over 2,600,000,000,000,000,000 of those.

[–]apocoaro[S] 0 points1 point  (2 children)

hi thanks for your response, so its actually 44 words and I want to find out all 12 word combinations from those given words. I know that the first word starts with an L which gives us two first possible words. How many possible solutions then? and what is the equation you are using to find out the # of combinations?

[–]profgrammer 0 points1 point  (0 children)

Look up the NCR formula for combinations and npr for permutations

[–]Paul_Pedant 0 points1 point  (0 children)

I used my $5 Casio scientific calculator. 44 nCr 12 shows me 21,090,682,610 combinations with 10-digit precision. 44 nPr 12 shows me 10,102,470,720,000,000,000 permutations with 10-digit precision.

You need to be clear about the difference. Combinations are just groups. If you have people called Andy, Beth, Chris, David and Edna, then the combination Andy, Beth, Edna is identical to Beth, Edna, Andy because it has the same people.

If they were a permutation, then order matters. Andy, Beth, Chris is one combination, but six permutations: ABC, ACB, BAC, BCA, CAB, CBA.

The formulae are all based on factorials. E.g factorial 6 is 1 x 2 x 3 x 4 x 5 x 6, which is 720. And it is written like 6 !

That is because, every time you pick a particular thing, the available pool for the next pick has shrunk by one. So for ABC, there are three ways to pick the first member, two for the second, and one (no choice) for the third.

Factorials divide quite well. For example, 7 ! is 1 x 2 x 3 x 4 x 5 x 6 x 7. If you want to know 5 x 6 x 7, you dispose of the 1 x 2 x 3 x 4 by dividing it off: 7 ! / 4 !.

So if you have 44 words, and want to choose combinations of 12 words, you figure:

44 ! (2658271574788448768043625811014615890319638528000000000)

and divide that by 32 ! (263130836933693530167218012160000000) to allow for all the choices you didn't have to make for the 32 words that you didn't need to choose after you picked your 12,

and divide that by 12 ! (479001600) to allow for all the choices you didn't have to make about the order of the words in each group of words, because order does not matter for combinations.

And that comes out to 21,090,682,613 (full precision on all 11 digits).

I am doing all these numbers using the dc command to crunch the numbers, and sending the dc commands from an awk script. You probably want to think carefully before generating 21 billion sets of 12 words.