Answer to Question #212666 in Python for sudheer

Question #212666

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)

    tuple_a=tuple(triplet)

    print(tuple_a)

input:0 1 2 3 5 7 13 17 19 19

22

output should be like this :

(0, 3, 19)

(0, 5, 17)

(1, 2, 19)

(2, 3, 17)

(2, 7, 13)


1
Expert's answer
2021-07-01T13:19:12-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))
print(*triplets_tuple, sep='\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