Code
Method 1:
def combine(A):
return [x for i in range(len(A)) for x in combineHelper(A, 0, i+1)]
def combineHelper(A, p, n):
if n == 0:
return [()]
else:
return [(A[i],) + x for i in range(p, len(A)) if (i + n - 1) < len(A) for x in combineHelper(A, i+1, n-1)]
def main():
A = [3, 1, 2]
print('the combination of A is: ', combine(A))
main()
Method 2:
def combine(A, p, n):
if n == 1:
return [(A[p],)]
else:
B = combine(A, p+1, n-1)
return [(A[p],) + x for x in B] + [(A[p],)] + B
def main():
A = [3, 1, 2]
print('the combination of A is: ', combine(A, 0, len(A)))
main()
Mathematical formula
![][1]
[1]: http://latex.codecogs.com/gif.latex?2n=(1+1)n=\sum_{k=0}^n\binom{n}{k}
![][2]
[2]: http://latex.codecogs.com/gif.latex?\binom{n}{m}=\frac{n!}{m!(n-m)!}