From f6e7fd604a9c639c1997b3bd49a1f484af92cd6a Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 14:08:57 -0500 Subject: [PATCH 1/8] changes --- dynamic_programming/subset_generation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 819fd8106def..588224b38b87 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -28,6 +28,7 @@ def combination_util(arr, n, r, index, data, i): # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() + # edit def print_combination(arr, n, r): From 886679ec1e4116890f38b84a9ce6c8507691b57d Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 15:43:41 -0500 Subject: [PATCH 2/8] doctest test --- dynamic_programming/subset_generation.py | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 588224b38b87..e0edc60c2fb9 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -9,6 +9,23 @@ 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 + + both of these should return nothing + >>> combination_util([], 3, 2, 0, [0, 0], 0) + + >>> combination_util([1], 3, 2, 0, [0, 0], 0) + + """ if index == r: for j in range(r): @@ -32,7 +49,25 @@ def combination_util(arr, n, r, index, data, i): def print_combination(arr, n, r): + """ + >>> print_combination([1, 2, 3], 3, 2, 0, [0, 0], 0) + 1 2 + 1 3 + 2 3 + + >>> print_combination([-1,-2,-3], 3, 2, 0, [0, 0], 0) + -1 -2 + -1 -3 + -2 -3 + + both of these should return nothing + >>> print_combination([], 3, 2, 0, [0, 0], 0) + + >>> print_combination([1], 3, 2, 0, [0, 0], 0)""" # A temporary array to store all combination one by one + # returns nothing if n < r + if n < r: + return data = [0] * r # Print all combination using temporary array 'data[]' combination_util(arr, n, r, 0, data, 0) @@ -40,6 +75,7 @@ def print_combination(arr, n, r): 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) # This code is contributed by Ambuj sahu From c2f28f5e9edcac6bcd9d188aadf23caa97bfdc7f Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 15:52:51 -0500 Subject: [PATCH 3/8] fixing 4 tests --- dynamic_programming/subset_generation.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index e0edc60c2fb9..61aa8cff7007 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -14,16 +14,14 @@ def combination_util(arr, n, r, index, data, i): 1 2 1 3 2 3 - >>> combination_util([-1,-2,-3], 3, 2, 0, [0, 0], 0) -1 -2 -1 -3 -2 -3 - both of these should return nothing - >>> combination_util([], 3, 2, 0, [0, 0], 0) + >>> combination_util([], 0, 2, 0, [0, 0], 0) - >>> combination_util([1], 3, 2, 0, [0, 0], 0) + >>> combination_util([1], 1, 2, 0, [0, 0], 0) """ @@ -36,6 +34,7 @@ def combination_util(arr, n, r, index, data, i): 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 @@ -50,23 +49,21 @@ def combination_util(arr, n, r, index, data, i): def print_combination(arr, n, r): """ - >>> print_combination([1, 2, 3], 3, 2, 0, [0, 0], 0) + >>> print_combination([1, 2, 3], 3, 2) 1 2 1 3 2 3 - - >>> print_combination([-1,-2,-3], 3, 2, 0, [0, 0], 0) + >>> print_combination([-1,-2,-3], 3, 2) -1 -2 -1 -3 -2 -3 - both of these should return nothing - >>> print_combination([], 3, 2, 0, [0, 0], 0) + >>> print_combination([], 3, 2) - >>> print_combination([1], 3, 2, 0, [0, 0], 0)""" + >>> print_combination([1], 3, 2)""" # A temporary array to store all combination one by one # returns nothing if n < r - if n < r: + if len(arr) < r: return data = [0] * r # Print all combination using temporary array 'data[]' @@ -76,6 +73,10 @@ def print_combination(arr, n, r): 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 From 9707673897106c16d624f9b4c625682d24d4d4c4 Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 15:59:07 -0500 Subject: [PATCH 4/8] added mixed types, different type, etc --- dynamic_programming/subset_generation.py | 28 +++++++++++++++++------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 61aa8cff7007..84d39b73ab02 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -18,7 +18,7 @@ def combination_util(arr, n, r, index, data, i): -1 -2 -1 -3 -2 -3 - both of these should return nothing + >>> combination_util([], 0, 2, 0, [0, 0], 0) >>> combination_util([1], 1, 2, 0, [0, 0], 0) @@ -26,9 +26,7 @@ def combination_util(arr, n, r, index, data, i): """ if index == r: - for j in range(r): - print(data[j], end=" ") - print(" ") + print(" ".join(map(str, data[:r]))) return # When no more elements are there to put in data[] if i >= n: @@ -60,7 +58,24 @@ def print_combination(arr, n, r): >>> print_combination([], 3, 2) - >>> print_combination([1], 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: @@ -73,9 +88,6 @@ def print_combination(arr, n, r): 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) From 54afdfab14e49c2d3725094cb9c457fef6e77a2e Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 16:37:24 -0500 Subject: [PATCH 5/8] added mixed types, different type and fixed typing --- .gitignore | 1 + dynamic_programming/subset_generation.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index baea84b8d1f1..8e3c757fa15b 100644 --- a/.gitignore +++ b/.gitignore @@ -86,6 +86,7 @@ celerybeat-schedule .venv env/ venv/ +myenv/ ENV/ env.bak/ venv.bak/ diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 84d39b73ab02..3c407d49986d 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -42,7 +42,6 @@ def combination_util(arr, n, r, index, data, i): # The main function that prints all combinations # of size r in arr[] of size n. This function # mainly uses combinationUtil() - # edit def print_combination(arr, n, r): From 26fb7aa8303b5a364987cdd994e6f01ead70121e Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 16:47:18 -0500 Subject: [PATCH 6/8] add the test for i --- dynamic_programming/subset_generation.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 3c407d49986d..9c7e93df8faf 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -23,6 +23,8 @@ def combination_util(arr, n, r, index, data, i): >>> combination_util([1], 1, 2, 0, [0, 0], 0) + >>> combination_util([1], 1, 2, 0, [0, 0], 3) + """ if index == r: @@ -87,7 +89,9 @@ def print_combination(arr, n, r): 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 From 8e9fde1192c03dc42ef48ef70015c5f552c7936a Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 16:51:54 -0500 Subject: [PATCH 7/8] removed doctest --- dynamic_programming/subset_generation.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index 9c7e93df8faf..f99eef236e75 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -89,9 +89,6 @@ def print_combination(arr, n, r): 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 From 99af4f575a47d1e6f605065de6d6f101ae333427 Mon Sep 17 00:00:00 2001 From: Aryan Gandhi Date: Sat, 14 Oct 2023 17:07:29 -0500 Subject: [PATCH 8/8] removed print in alg --- dynamic_programming/subset_generation.py | 77 +++++++++++------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/dynamic_programming/subset_generation.py b/dynamic_programming/subset_generation.py index f99eef236e75..14fc0592b986 100644 --- a/dynamic_programming/subset_generation.py +++ b/dynamic_programming/subset_generation.py @@ -1,7 +1,7 @@ # 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 @@ -10,71 +10,59 @@ def combination_util(arr, n, r, index, data, i): 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([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([], 0, 2, 0, [0, 0], 0, []) - >>> combination_util([1], 1, 2, 0, [0, 0], 0) - - >>> combination_util([1], 1, 2, 0, [0, 0], 3) + >>> combination_util([1], 1, 2, 0, [0, 0], 0, []) + [] """ + if index == r: - print(" ".join(map(str, data[:r]))) - 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 + 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() - 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() + return results def print_combination(arr, n, r): """ >>> print_combination([1, 2, 3], 3, 2) - 1 2 - 1 3 - 2 3 + [[1, 2], [1, 3], [2, 3]] >>> print_combination([-1,-2,-3], 3, 2) - -1 -2 - -1 -3 - -2 -3 + [[-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 + [[1, 2], [1, 2], [2, 2]] >>> print_combination(['a', 'b', 'c'], 3, 2) - a b - a c - b c + [['a', 'b'], ['a', 'c'], ['b', 'c']] >>> print_combination([1, 'a', 2], 3, 2) - 1 a - 1 2 - a 2 + [[1, 'a'], [1, 2], ['a', 2]] """ # A temporary array to store all combination one by one @@ -83,12 +71,17 @@ def print_combination(arr, n, 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