-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum solution.py
More file actions
66 lines (61 loc) · 1.74 KB
/
Copy pathCombination Sum solution.py
File metadata and controls
66 lines (61 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#code
from itertools import combinations
#function to remove repeated sublists in a linst of sublists
def Remove(lst):
return ([list(i) for i in {*[tuple(sorted(i)) for i in lst]}])
#function to get all sublists from parent list
def sub_lists(my_list):
subs = []
for i in range(0, len(my_list)+1):
temp = [list(x) for x in combinations(my_list, i)]
if len(temp)>0:
subs.extend(temp)
return subs
#function to check the condition of the question
#Here sum of elements of a list should be equal to the number entered by user
def sublistspossible(arr,n,a):
semifinal=[]
final=[]
# semifinal.clear()
# final.clear()
semifinal=sub_lists(arr)
p=len(semifinal)
for i in range(0,p,1):
if(sum(semifinal[i])==a):
final.append(semifinal[i])
else:
pass
if len(final)==0:
final.append("Empty")
return final
#function to print the result in a proper sequence as accepted in the terminal
def printelements(e):
count=0
if e!="Empty":
ans="("
for i in (e):
count+=1
if count==len(e):
ans+=str(i)
else:
ans+=str(i)+" "
ans=ans+")"
return ans
else:
ans="Empty"
return ans
if __name__ == '__main__':
t=int(input())
for _ in range(t):
n=int(input())
arr=[int(x) for x in input().strip().split()]
arr.sort()
a=int(input())
ans=sublistspossible(arr,n,a)
if ans[0]!="Empty":
ans=Remove(ans)
ans.sort() #sorting the sublists inside the list in ascending order
for i in ans:
print(printelements(i),end="")
print()
ans.clear()