Answer to Question #320219 in Python for ali

Question #320219

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


1
Expert's answer
2022-03-30T14:10:40-0400
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}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS