honkert
honkert

Reputation: 11

Can't understand subset creator

New with programming and spend a lot of time on trying to understand this recursive function, but I just can't visualize it. It's about the subsetsRecur function. Anyone have a simple understandable explanation?

class py_solution:

    def sub_sets(self, sset):
        return self.subsetsRecur([], sorted(sset))

    def subsetsRecur(self, current, sset):
        if sset:
            return self.subsetsRecur(current, sset[1:]) + self.subsetsRecur(current + [sset[0]], sset[1:])
        return [current]
print(py_solution().sub_sets([4,5,6]))

Upvotes: 0

Views: 126

Answers (1)

blue note
blue note

Reputation: 29099

The code calculates the powerset of a given set. It is a direct implementation of a standard recursive algorithm. See here for the definition of the algorithm

Upvotes: 1

Related Questions