Print the power set of a given set of distinct integers.

Given a set of distinct integers, this program prints out the power set of the set. The power set of a set is the set of all its subsets.

Problem

Given a set of distinct integers, print the power set of the set.
The power set of a set is the set of all its subsets.
Input:
Set = {1, 2, 3}
Output:
{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}

Solution

This solution is in JavaScript. Our A.I. can create solutions in multiple languages.
by kayakane
// Solution:
// The optimal solution is to use a recursive function that takes in the set and an empty array.
// The function will then iterate through the set and add each element to the array.
// The function will then call itself with the set and the array with the element added.
// The function will then call itself with the set and the array without the element added.
// This will create a tree of all the possible combinations of the set.
// The function will then return the array.

function powerSet(set, array = []) {
  for (let i = 0; i < set.length; i++) {
    array.push(set[i]);
    powerSet(set, array);
    array.pop();
  }
  return array;
}

console.log(powerSet([1, 2, 3]));

A.I. Evaluation of the Solution

The candidate's solution is a good start, but it is not complete. The solution does not actually print the power set, it only returns it. To print the power set, the candidate would need to add a line to print the array at the end of the function. Additionally, the candidate's solution only works for sets with distinct integers. To make the solution work for sets with non-distinct integers, the candidate would need to add additional logic to check for duplicates.

Evaluated at: 2022-11-20 04:15:55