Find all subsets of a set of distinct integers

This problem asks to return all possible subsets of a given set of distinct integers. For example, for the input [1,2,3], the output would be all possible subsets of those integers: [1], [2], [3], [1,2], [1,3], [2,3], and [1,2,3].

Problem

Given a set of distinct integers, return all possible subsets.
Example input: [1,2,3]
Example 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
# Solution:
# The solution is to use a recursive function that takes in a list of integers and a list of lists.
# The function will iterate through the list of integers and add each integer to the list of lists.
# Then it will call itself with the list of integers minus the current integer and the list of lists.
# This will continue until the list of integers is empty.
# The function will then return the list of lists.
# This solution is optimal because it is O(2^n) time complexity and O(n) space complexity.

def subsets(nums):
    return subsets_helper(nums, [])

def subsets_helper(nums, lists):
    if len(nums) == 0:
        return lists
    else:
        for i in range(len(nums)):
            lists.append([nums[i]])
            lists = subsets_helper(nums[i+1:], lists)
        return lists

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 use a recursive function that takes in a list of integers and a list of lists. The function will iterate through the list of integers and add each integer to the list of lists. Then it will call itself with the list of integers minus the current integer and the list of lists. This will continue until the list of integers is empty. The function will then return the list of lists. This solution is optimal because it is O(2^n) time complexity and O(n) space complexity.

Evaluated at: 2022-11-17 23:34:24