By both generating and directly computing obtain the number of the following:
a. All permutations of (0,1,2,3,4,5).
b. All permutations of ("A","B","C").
c. Permutations of size 3 of (0,1,2,3,4,5).
d. Permutations of size 2 of (0,1,2,3,4,5,6).
e. Combinations of size 3 of (0,1,2,3,4,5).
f. Combinations of size 2 of (0,1,2,3,4,5).
g. Combinations of size 5 of (0,1,2,3,4,5)
def permutation(L, n):
if n == 0:
yield tuple()
return
for i in range(len(L)):
x = L[i]
LL = L[:i] + L[i+1:]
for P in permutation(LL, n-1):
yield tuple([x] + list(P))
def combination(L, n):
if n == 1:
for x in L:
yield (x,)
else:
for i in range(len(L)+1 - n):
x = L[i]
for C in combination(L[i+1:], n-1):
yield tuple([x] + list(C))
print('=========== a ============')
L = (0, 1, 2, 3, 4, 5)
cnt = 0
for p in permutation(L, len(L)):
cnt += 1
print(f'The number of all permutations of {L} is {cnt}')
print()
print('=========== b ============')
L = ("A", "B", "C")
cnt = 0
for p in permutation(L, len(L)):
cnt += 1
print(f'The number of all permutations of {L} is {cnt}')
print()
print('=========== c ============')
L = (0, 1, 2, 3, 4, 5)
n = 3
cnt = 0
for p in permutation(L, n):
cnt += 1
print(f'The number of permutations of size {n} of {L} is {cnt}')
print()
print('=========== d ============')
L = (0, 1, 2, 3, 4, 5, 6)
n = 2
cnt = 0
for p in permutation(L, n):
cnt += 1
print(f'The number of permutations of size {n} of {L} is {cnt}')
print()
print('=========== e ============')
L = (0, 1, 2, 3, 4, 5)
n = 3
cnt = 0
for c in combination(L, n):
cnt += 1
print(f'The number of combinations of size {n} of {L} is {cnt}')
print()
print('=========== f ============')
L = (0, 1, 2, 3, 4, 5)
n = 2
cnt = 0
for c in combination(L, n):
cnt += 1
print(f'The number of combinations of size {n} of {L} is {cnt}')
print()
print('=========== g ============')
L = (0, 1, 2, 3, 4, 5)
n = 5
cnt = 0
for c in combination(L, n):
cnt += 1
print(f'The number of combinations of size {n} of {L} is {cnt}')
Comments
Leave a comment