Skip to content

Adding doctests for sum_of_subset.py #1333

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

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions dynamic_programming/sum_of_subset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
def isSumSubset(arr, arrLen, requiredSum):

"""
>>> isSumSubset([2, 4, 6, 8], 4, 5)
False
>>> isSumSubset([2, 4, 6, 8], 4, 14)
True
"""
# a subset value says 1 if that subset sum can be formed else 0
# initially no subsets can be formed hence False/0
subset = [[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)]
Expand All @@ -22,14 +27,9 @@ def isSumSubset(arr, arrLen, requiredSum):
# uncomment to print the subset
# for i in range(arrLen+1):
# print(subset[i])
print(subset[arrLen][requiredSum])
Copy link
Member

@cozek cozek Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary . Try this.
take test.py as

def returns_true():
    """
    >>> returns_true()
    True
    """
    return True

then
python3 -m doctest test.py -v

However, thank you adding the doctests.


return subset[arrLen][requiredSum]

if __name__ == "__main__":
import doctest

arr = [2, 4, 6, 8]
requiredSum = 5
arrLen = len(arr)
if isSumSubset(arr, arrLen, requiredSum):
print("Found a subset with required sum")
else:
print("No subset with required sum")
doctest.testmod()