1. Description of the problem
How about a combination of 4 numbers for 9 numbers and a list of combinations?
We can consider a simple combination of the following: from 1,2,3,4,5,6, how to select any combination of four numbers.
Fixed: 1 2 3, combination 1234 1235 1236
Fixed 1 2 4, combination: 1245 1246
Fixed 1 2 5, combination: 1256
Fixed 1 3 4, combination: 1345 1346
Fixed 1 3 5, combination: 1356
Fixed 1 4 5, combination: 1456
Fixed 2 3 4, combination: 2345 2346
Fixed 2 3 5, combination: 2356
Fixed 2 4 5, combination: 2456
Fixed 3 4 5, combination: 3456
A total of 15 combinations
2. Simple implementation of the combinatorial algorithm
First, we analyze the 10 steps listed above, for 1), it is clear that after fixing 1 2 3 of these three numbers, the fourth number is 4, 5, 63 numbers are cycled (remember the loop, the computer is best at doing loops). Second, we re-analyze 1) 2) 3) The fixed number in the three steps, the first two unchanged, are 1 2, just the third number in the loop (again loop).
Through analysis, we can find such a pattern:
1 a fixed 3 number, let the fourth number loop
2 Let the third number loop, repeat the first step
3 Let the second number loop, repeat the first step
4 Let the first number loop, repeat the first step
defCOMBNUMBERLOOP4 (M, b): Totalnumber=0 forIinchRange (1, m+2-4): b[0]=I forJinchRange (i+1, m+2-3): b[1] =J forKinchRange (j+1, m+2-2): b[2] =k forLinchRange (k+1, m+2-1): b[3] =LPrintb totalnumber+ = 1returnTotalnumber Group=[99,99,99,99]Print "\nusing Loop:%d\n"% COMBNUMBERLOOP4 (6, group)
The output of the program, as previously analyzed.
3. How to solve combinatorial problems recursively
Let's go back to the C (6,4) question, and in addition to the 10 steps listed above, let's take a different approach:
If you select a number in the 6 number, then determine whether the remaining 3 number is changed to "a combination of 3 numbers from 5 numbers"? And so on, this seems to be a "recursive" problem, indeed, we can use a recursive approach to solve the problem.
For C (m, N) List all the combinations of problems, you can follow the steps to program, this time we press the forward order from the back to list all the combinations:
1 Select an element I, loop within the range [M, n]
2 I as the last element of the combination
3 If n-1>0, make a recursive call, C (M-1, n-1); otherwise, the output
4 Repeating ①
Note that the design recursive function must have a termination condition, otherwise it will cause a "dead loop".
The implementation code is as follows:
defCombnumber (M, N, b):GlobalTotalnumberr forIinchRange (M, n-1, 1): B[n-1] =IifN-1>0:combnumber (i-1,n-1, B)Else: Printb totalnumberr+ = 1returnTotalnumberr Group=[99,99,99,99,99]totalnumberr=0Print "\nusing Recursive:%d\n"% Combnumber (7,5,group)
The use of recursion, so that the program is very concise writing, but, in the writing process, I have two places (in fact, the program is difficult in these two places), tossing for several days to find out:
The first is how the loop range of the For loop is determined;
The second is the parameter design used in recursive invocation;
Built-in composite functions provided by 3.python
defcombinations_with_replacement (iterable, R):#combinations_with_replacement (' ABC ', 2)--AA AB AC BB BC CCPool =tuple (iterable) n=len (Pool)if notN andr:returnIndices= [0] *RyieldTuple (Pool[i] forIinchindices) whileTrue: forIinchReversed (range (r)):ifIndices[i]! = n-1: Break Else: returnIndices[i:]= [Indices[i] + 1] * (R-i)yieldTuple (Pool[i] forIinchIndices4. More Combinatorial Algorithms
Please click on me: https://docs.python.org/3/library/itertools.html?highlight=combinations#itertools.combinations
Python solves combinatorial problems