Answer to Question #212627 in Python for sudheer

Question #212627

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)):

      a = integers[i]

      b = integers[j]

      c = integers[k]

      if a+b+c == K:

        triplets.append(tuple((a,b,c))) input:0 12 17 8 9 21

sorted_triplets=[] 29

for tripl in triplets:

  item = sorted(tripl) output:[0, 8, 21]

  sorted_triplets.append(item) [0, 12, 17]

triplets= sorted(sorted_triplets) [8, 9, 12]

if len(triplets)!=0:

  for triplet in triplets: output should be like this:(0, 8, 21)

    triplet=sorted(triplet) (0, 12, 17)

    print(triplet) (8, 9, 12)

else: output should be in round braces

  print("No Matching Triplets Found")



1
Expert's answer
2021-07-02T04:10:24-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)):


            a = integers[i]


            b = integers[j]


            c = integers[k]


            if a+b+c == K:


                triplets.append(tuple((a,b,c)))


                sorted_triplets=[] 


                for tripl in triplets:


                    item = sorted(tripl) 


                    sorted_triplets.append(item) 


                triplets= sorted(sorted_triplets) 


                if len(triplets)!=0:


                    for triplet in triplets: 


                        triplet=sorted(triplet) 


                        print(triplet) 


                else: 


                      print("No Matching Triplets Found")

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