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 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
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
77 changes: 60 additions & 17 deletions dynamic_programming/subset_generation.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,87 @@
# Print all subset combinations of n element in given set of r element.


def combination_util(arr, n, r, index, data, i):
def combination_util(arr, n, r, index, data, i, results):
"""
Current combination is ready to be printed, print it
arr[] ---> Input Array
data[] ---> Temporary array to store current combination
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, [])
[]


"""

if index == r:
for j in range(r):
print(data[j], end=" ")
print(" ")
return
results.append(data[:r])

# When no more elements are there to put in data[]
if i >= n:
elif 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
# next (Note that i+1 is passed, but
# index is not changed)
combination_util(arr, n, r, index, data, i + 1)
# The main function that prints all combinations
# of size r in arr[] of size n. This function
# mainly uses combinationUtil()
else:
data[index] = arr[i]
combination_util(arr, n, r, index + 1, data, i + 1, results)
# current is excluded, replace it with
# next (Note that i+1 is passed, but
# index is not changed)
combination_util(arr, n, r, index, data, i + 1, results)
# The main function that prints all combinations
# of size r in arr[] of size n. This function
# mainly uses combinationUtil()

return results


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)
results = []
(combination_util(arr, n, r, 0, data, 0, results))
return results


if __name__ == "__main__":
# Driver code to check the function above
# arr = [10,20,30,40,50]
arr = [10, 20, 30, 40, 50]
print_combination(arr, len(arr), 3)
results = print_combination(arr, len(arr), 3)

for combo in results:
print(" ".join(map(str, combo)))
# This code is contributed by Ambuj sahu