All Possible Subsets of a Set of Distinct Integers

Given a set of distinct integers, this algorithm returns all possible subsets. The subsets must be in non-descending order, and the solution set must not contain duplicate subsets.

Problem

Given a set of distinct integers, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def subsets(S):
    S.sort()
    result = [[]]
    for i in range(len(S)):
        for j in range(len(result)):
            result.append(result[j] + [S[i]])
    return result

print subsets([1,2,3])

A.I. Evaluation of the Solution

The candidate's solution is correct and solves the problem. The approach is to first sort the given set, then create all possible subsets by iterating through the set and adding each element to all existing subsets.

Evaluated at: 2022-12-07 12:15:39