Power Set of a Set of Distinct Integers

The problem asks us to print the power set of a given set of distinct integers. A power set is the set of all subsets of a given set, including the empty set and the original set itself. For example, the power set of {1,2,3} would be {{},{1},{2},{3},{1,2},{1,3 },{2,3},{1,2,3}}.

Problem

Given a set of distinct integers, print the power set of the set.
Input: {1,2,3}
Output: {{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
# The optimal solution is to use a recursive function to generate the power set.
# The function takes a set and a list of sets as input.
# The function adds the empty set to the list of sets.
# The function then iterates through the set and adds each element to the list of sets.
# The function then iterates through the set and calls itself with the set minus the current element and the list of sets.
# The function then returns the list of sets.

def power_set(s, l):
    l.append(set())
    for e in s:
        l.append(set([e]))
    for e in s:
        power_set(s - set([e]), l)
    return l

print(power_set(set([1,2,3]), []))

A.I. Evaluation of the Solution

This is a good solution that demonstrates a level of completeness and solves the problem. The approach is recursive and uses a set and a list to keep track of the elements in the power set.

Evaluated at: 2022-11-12 06:16:07