Skip to content

Instantly share code, notes, and snippets.

@ctkqiang
Last active June 13, 2021 06:13
Show Gist options
  • Save ctkqiang/4a0bc5bde9e846da41239c87cee073c9 to your computer and use it in GitHub Desktop.
Save ctkqiang/4a0bc5bde9e846da41239c87cee073c9 to your computer and use it in GitHub Desktop.
class Subsettest:
def __init__(self, s, t):
self.s = s
self.t = t
n = len(s) # Get The Length of S
if(self.issubsetsum(s, n, t) == True):
print("The sum of the subset is equal to T")
else:
print("The sum of the subset is not equal to T")
def issubsetsum(self, s, n, t):
if (t == 0):
return True
if (n == 0):
return False
# Element is greater than
# the sum return null
if s[n - 1] > t:
return self.issubsetsum(s, n-1, t)
return self.issubsetsum(
s, n-1, t or
self.issubsetsum(s, n-1, t - s[n-1])
)
if __name__ == "__main__":
s = [3, 34, 4, 12, 5, 2]
t = 9
test_subset_sum = Subsettest(s, t)
#uncomment for user input:
# k = input("Enter an Array of Number")
# l = input("Enter an Integer")
# test_subset_sum = Subsettest(k, l)
"""algo
START
declare S,N, T;
declare value of S, T;
a method for ispecting ?T = Sum;
END
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment