From 6f63ee7b02799e29e6fce3ef6fa3e6e1eba43c27 Mon Sep 17 00:00:00 2001 From: Ankit Akash <75488501+ankit-akash@users.noreply.github.com> Date: Sat, 15 Oct 2022 23:29:12 +0530 Subject: [PATCH] Create sum_of_subsets.py --- sum_of_subsets.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 sum_of_subsets.py diff --git a/sum_of_subsets.py b/sum_of_subsets.py new file mode 100644 index 00000000..1badc132 --- /dev/null +++ b/sum_of_subsets.py @@ -0,0 +1,26 @@ +#Sum of subsets problem in python. + +b=[] +def solution(a,n,subset,s): + global ctr + ctr=0 + if s==0: + b.append(subset) + return + if n==0: + return + if a[n-1]<=s: + solution(a,n-1,subset,s) + solution(a,n-1,subset+str(a[n-1])+" ",s-a[n-1]) + else: + solution(a,n-1,subset,s) +print("Enter the list of integers",end=":") +a=[int(x) for x in input().split()] +s=int(input("Enter the sum value:")) +subset="" +solution(a,len(a),subset,s) +ctr=1 +for i in b: + print("Solution",ctr,"---->",i) + ctr=ctr+1 +print("The total number of solution for sum of subsets is",len(b))