Printing the power set of a set of distinct integers.

This problem deals with printing the power set of a given set of distinct integers. The power set is the set of all subsets of a given set, including the empty set and the original set itself. In this case, the input is {1, 2, 3} and the output should 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 C++. Our A.I. can create solutions in multiple languages.
by kayakane
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void print_vector(vector<int> v) {
    cout << "{";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i];
        if (i != v.size() - 1) {
            cout << ", ";
        }
    }
    cout << "}";
}

void print_power_set(vector<int> v) {
    vector<vector<int>> power_set;
    power_set.push_back(vector<int>());
    for (int i = 0; i < v.size(); i++) {
        int n = power_set.size();
        for (int j = 0; j < n; j++) {
            vector<int> temp = power_set[j];
            temp.push_back(v[i]);
            power_set.push_back(temp);
        }
    }
    for (int i = 0; i < power _set.size(); i++) {
        print_vector(power_set[i]);
        cout << endl;
    }
}

int main() {
    vector<int> v = {1, 2, 3};
    print_power_set(v);
    return 0;
}

A.I. Evaluation of the Solution

The candidate's solution correctly generates the power set of a given set of distinct integers. The solution is complete and correctly solves the problem. The approach is straightforward and easy to follow.

Evaluated at: 2022-11-27 04:16:03