Skip to content

Dp subset tests Closes #9943 #10471

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

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ celerybeat-schedule
.venv
env/
venv/
myenv/
ENV/
env.bak/
venv.bak/
Expand Down
59 changes: 56 additions & 3 deletions dynamic_programming/subset_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@ def combination_util(arr, n, r, index, data, i):
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed

>>> combination_util([1, 2, 3], 3, 2, 0, [0, 0], 0)
1 2
1 3
2 3
>>> combination_util([-1,-2,-3], 3, 2, 0, [0, 0], 0)
-1 -2
-1 -3
-2 -3

>>> combination_util([], 0, 2, 0, [0, 0], 0)

>>> combination_util([1], 1, 2, 0, [0, 0], 0)

>>> combination_util([1], 1, 2, 0, [0, 0], 3)


"""
if index == r:
for j in range(r):
print(data[j], end=" ")
print(" ")
print(" ".join(map(str, data[:r])))
Copy link
Member

@cclauss cclauss Oct 14, 2023

Choose a reason for hiding this comment

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

CONTRIBUTING.md says that algorithmic functions should not print() instead they should return values or yield values or raise exceptions.

return
# When no more elements are there to put in data[]
if i >= n:
return
# current is included, put next at next location

data[index] = arr[i]
combination_util(arr, n, r, index + 1, data, i + 1)
# current is excluded, replace it with
Expand All @@ -31,14 +47,51 @@ def combination_util(arr, n, r, index, data, i):


def print_combination(arr, n, r):
"""
>>> print_combination([1, 2, 3], 3, 2)
1 2
1 3
2 3
>>> print_combination([-1,-2,-3], 3, 2)
-1 -2
-1 -3
-2 -3

>>> print_combination([], 3, 2)

>>> print_combination([1], 3, 2)

>>> print_combination([1, 2, 2], 3, 2)
1 2
1 2
2 2

>>> print_combination(['a', 'b', 'c'], 3, 2)
a b
a c
b c

>>> print_combination([1, 'a', 2], 3, 2)
1 a
1 2
a 2

"""
# A temporary array to store all combination one by one
# returns nothing if n < r
if len(arr) < r:
return
data = [0] * r
# Print all combination using temporary array 'data[]'
combination_util(arr, n, r, 0, data, 0)


if __name__ == "__main__":
# Driver code to check the function above
# arr = [10,20,30,40,50]
import doctest

doctest.testmod()
arr = [10, 20, 30, 40, 50]
print_combination(arr, len(arr), 3)
# This code is contributed by Ambuj sahu