0

I'm returning true after a copy still i'm getting all sequences.

def subset(nums,tot):
    res=[]
    def sub_seq(curr,i,s):
        if i==len(nums):
            if s==tot:
                res.append(curr.copy())
                return True# Even I'm returning true its bot working
            return False
                
        curr.append(nums[i])
        s+=nums[i]
        sub_seq(curr,i+1,s)
                
        s-=nums[i]
        curr.pop()
        sub_seq(curr,i+1,s)
    sub_seq([],0,0)
    return res
    
print(subset([1,2,1],2))
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • There's no `return` at the end of `sub_seq`. – Stef Apr 20 '22 at 13:58
  • If I understand correctly, you're trying to implement a bruteforce algorithm, which tries to build a subsequence with `nums[i]` recursively, then if that doesn't work, tries to build a subsequence without `nums[i]`? – Stef Apr 20 '22 at 14:01
  • `res.append(curr.copy())` should probably be `res.extend(curr)` instead. – Stef Apr 20 '22 at 14:02
  • @Stef: That just flattens the result (and loses information on which numbers belong to which subsequence). The result of the OP's code is `[[1, 1], [2]]` right now (both are legal subsequences summing to `2`), your change makes it `[1, 1, 2]` (which numbers go together, who knows?). – ShadowRanger Apr 20 '22 at 14:05
  • @ShadowRanger I was under the impression the OP only wanted to return one subsequence. But you're saying they want to return a list of subsequences instead? – Stef Apr 20 '22 at 14:26
  • @Stef: They only want one subsequence, but making your change without fixing the actual current problem (where it returns all the subsequences, e.g. returning `[[1, 1], [2]]` when the OP wants `[[1, 1]]`) means all the subsequences get blended. Once that problem is fixed, that might be *part* of the solution (depending on how the OP wishes to report a lack of any such subsequences; it's possible they want to return a `list` that's either empty or contains a single sublist to remain API compatible with some other function that returns all the subsequences for instance). – ShadowRanger Apr 20 '22 at 14:45
  • @ShadowRanger sure – Stef Apr 20 '22 at 15:18

1 Answers1

0

You're returning True, but you're not using it to stop further processing. To do so, have the other recursive calls to sub_seq immediately cease continued processing when the recursive call returns True:

def subset(nums,tot):
    res=[]
    def sub_seq(curr,i,s):
        if i==len(nums):
            if s==tot:
                res.append(curr.copy())
                return True
            return False

        curr.append(nums[i])
        s+=nums[i]
        if sub_seq(curr, i+1, s):   # Stop immediately if it returned True and propagate the True
            return True

        s-=nums[i]
        curr.pop()
        return sub_seq(curr,i+1,s)  # Propagate whatever the recursive call returned
    sub_seq([],0,0)
    return res

print(subset([1,2,1],2))

Try it online!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271