-
Notifications
You must be signed in to change notification settings - Fork 8
Add subsetsum check #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def isSubsetSum (items, length, summ): | ||
if summ == 0: | ||
return True | ||
if length == 0 and summ != 0: | ||
return False | ||
|
||
if (items[length - 1] > summ): | ||
return isSubsetSum(items, length - 1, summ); | ||
|
||
return isSubsetSum(items, length - 1, summ) or isSubsetSum(items, length - 1, summ - items[length - 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the code runs in an exponential time. Please try to optimize using Dynamic programming. |
||
|
||
if __name__ == '__main__': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write the test cases in a separate file under the test directory. Check out other test cases which use the pytest to do the testing. |
||
data = [1, 3, 5, 9, 12, 6, 2] | ||
summ = 11 | ||
length = len(data) | ||
|
||
if (isSubsetSum(data, length, summ) == True): | ||
print('This set has subsetsum') | ||
else: | ||
print('No subsetsum') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide the comment stating what does the module/function do. Also, include the doctest in the comments.
Doctest reference - https://docs.python.org/2/library/doctest.html