Hello all,

I need some help creating codes to make a powerset...

Here is the skeleton:

public static StringList powerRecursive (String s) {

}

public static StringList powerLooping (String s) {

}

Thanks!

Printable View

- 04-20-2011, 02:06 AMcsisdifficultRecursive and Non-Recursive powersets?
Hello all,

I need some help creating codes to make a powerset...

Here is the skeleton:

public static StringList powerRecursive (String s) {

}

public static StringList powerLooping (String s) {

}

Thanks! - 04-20-2011, 02:14 AMozzyman
If S is the set {x, y, z}, then the subsets of S are:

{} (also denoted , the empty set)

{x}

{y}

{z}

{x, y}

{x, z}

{y, z}

{x, y, z}

Is this what you are trying to do? - 04-20-2011, 02:16 AMcsisdifficultYup!
Basically, but all in one string:

["", a, b, ab, ba, abab, baba] - 04-20-2011, 02:34 AMcsisdifficult
Please? Someone help?

- 04-20-2011, 02:40 AMozzyman
In one List, of course.

Something to start you off:

Code:`//define a List because you don't know how many items you need to store in it`

List<String> powerSet = new ArrayList<>();

//define set to make powerSet from

char[] aSet = new char[] {'a', 'b', 'c'};

//loop through each character

//define a String to store full set

String fullSet = "";

//add blank to List

powerSet.add(fullSet);

for (int i=0; i<aSet.length; i++) {

//add each item in set

powerSet.add(aSet[i]);

//compile the full set

fullSet += aSet[i];

}

//add full set

powerSet.add(fullSet);

So far this would get us everything except the 'ab', 'ac' and 'bc'.

ab, ac, bc... is like this: aa, ab, ac, ba, bb, bc, ca, cb, cc

but with same letter combos removed (aa, bb, cc)

and doubles removed (ab = ba, ac = ca, bc = cb) - 04-20-2011, 02:45 AMcsisdifficult
That sort of helps, I think I got it. Thanks!