Answer to Question #213117 in Python for Sudheer

Question #213117

integers = [int(number) for number in input().split()]

K = int(input())

triplets = []

for i in range(len(integers)):

for j in range(i+1,len(integers)):

for k in range(j+1, len(integers)):

tmp = tuple(sorted([integers[i], integers[j], integers[k]]))

if sum(tmp) == K:

if tmp not in triplets:

triplets.append(tmp)

triplets_tuple = tuple(sorted(triplets))

print(*triplets_tuple, sep='\n')

It should also print if there is no triplets matching it should print

Input:

1 5 6 3 5 11

30

Output:

No Matching Triplets Found




1
Expert's answer
2021-07-03T15:11:40-0400
integers = [int(number) for number in input().split()]
K = int(input())
triplets = []
for i in range(len(integers)):
    for j in range(i+1,len(integers)):
        for k in range(j+1, len(integers)):
            tmp = tuple(sorted([integers[i], integers[j], integers[k]]))
            if sum(tmp) == K:
                if tmp not in triplets:
                    triplets.append(tmp)
triplets_tuple = tuple(sorted(triplets))
if(len(triplets_tuple)>0):
    print(*triplets_tuple,sep='\n')
else:
    print('No Matching Triplets Found\n')

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