Answer to Question #208209 in Python for Sudheer

Question #208209

K Sum Unique Combinations


Given a list of integers, write a program to print the count of all possible unique combinations of numbers whose sum is equal to K.


Input


The first line of input will contain space-separated integers. The second line of input will contain an integer, denoting K.


Output


The output should be containing the count of all unique combinations of numbers whose sum is equal to K.


1
Expert's answer
2021-06-18T02:54:39-0400
from itertools import combinations
numbers = list(map(int, input().split()))
k = int(input())
result = 0 
for size in range (1, len(numbers)):
    all_comb = [comb  for comb in combinations(numbers, size) if sum(comb) == k]
    uniq_comb =[]
    for comb in all_comb:
        for uniq in uniq_comb:
            if not(set(comb) ^ set(uniq)):
                break
        else:
            uniq_comb.append(comb)
    result += len(uniq_comb)
print(result)    

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