I was solving the classic powerset generation using backtracking. Now, let's say, I add another constraint, the output should appear in a specific order.
For input = [1, 2, 3], the output should be [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]
My general solution (which generates the superset but not in any specific order):
class Solution:
def build_subsets(self, cur_i = 0, cur_ls = []):
if cur_i == len(self.nums):
self.output.append(cur_ls)
return # this is mendatory
self.build_subsets(cur_i + 1, cur_ls)
self.build_subsets(cur_i + 1, cur_ls + [self.nums[cur_i]])
def subsets(self, nums):
self.nums = nums
self.output = []
self.build_subsets()
return self.output
How can I change my backtracking code to find the output in the specific order (subsets with a lower number of elements appear earlier and they are sorted [for a group of a fixed number of elements])?
Update:
The following code which uses combinations function satisfy the condition. Now, the question becomes how can I write combinations using backtracking as I want to understand the recursive process for generating such a list.
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
Your code has almost everything you need to generate the subsets in the desired order. The key missing piece is the
forloop that determines the length of the subsets, as seen in the code that usescombinations. That loop starts by generating the subsets of length 0. It then continues with successively larger subsets until it reaches the final subset that includes all of the numbers.So you need to add a similar
forloop to thesubsetsfunction, and the desired length needs to be passed to thebuild_subsetsfunction. Thebuild_subsetsfunction then needs to terminate the recursion when the subset reaches the desired length.Other changes that are needed are:
With those changes, the code looks like this: