Three letters are selected at random from the 8 letters of the word COMPUTER, without regard
to order.a. Find the number of possible selections of 3 letters.
b. Find the number of selections of 3 letters with the letter P.
c. Find the number of selections of 3 letters where the 3 letters form the word TOP
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 [x] + C
word = 'COMPUTER'
L = [ch for ch in word]
n = 3
print(f'For the word {word}\n')
print('=========== a ============')
cnt = 0
for c in combination(L, n):
cnt += 1
print(f'The number of possible selections of {n} letters is {cnt}')
print()
print('=========== b ============')
letter = 'P'
cnt = 0
for c in combination(L, n):
if letter in c:
cnt += 1
print(f'The number of selections of {n} letters with the letter {letter} is {cnt}')
print()
print('=========== c ============')
word2 = 'TOP'
s2 = {ch for ch in word2}
cnt = 0
for c in combination(L, n):
if set(c) == s2:
cnt += 1
print(f'The number of selections of {n} letters where the {len(word2)}'
+ f' letters form the word {word2} is {cnt}')
Comments
Leave a comment