Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 8th, 2012, 12:03 PM
 ayase
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 33 m 48 sec
Reputation Power: 0
Homework - Returning a set of objects for powerset recursion

I can implement a powerset generator using an ArrayList but returning a set of Objects is beyond me.

Here is how I must implement it. MySet is just an implementation of Set (it works).

Code:
```static MySet<MySet<Object>> powerSet(MySet<Object> s) {

}```

Also there's powerset within the MySet given to me, again I'm not exactly sure what to make of it:

Code:
```
public MySet<MySet<T>> powerset(MySet<T> s) {
MySet<MySet<T>>  ps = new MySet<MySet<T>>();
if (s.cardinality()==0) {
return ps;
}
T a = s.takeOne();
ps = powerset(s);
int n = ps.cardinality();
for (int i=0; i<n;i++) {
MySet<T> t = new MySet<T>(ps.set[i]);
}
return ps;
}```

Thank you really for any insights

#2
November 8th, 2012, 04:20 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,609
Time spent in forums: 2 Weeks 3 Days 5 h 57 m 34 sec
Reputation Power: 347
If you are getting error messages, please copy the full text of the messages and paste it here.
Otherwise please explain what the problem is.

#3
November 9th, 2012, 12:45 PM
 ayase
Registered User

Join Date: Nov 2012
Posts: 2
Time spent in forums: 33 m 48 sec
Reputation Power: 0
adding an iterator and resetting s to it's inputted form on return with s2 (to account for adding new values) fixed it

complete result here:

Code:
```static MySet<MySet<Object>> powerSet(MySet<Object> s) {

MySet<MySet<Object>> ps = new MySet<MySet<Object>>();
MySet<Object> s2 = new MySet<Object>();

//base case
if (s.cardinality()==0) {
return ps;
}

Object a = s.takeOne();

ps = powerSet(s);
int n = ps.cardinality();

MySet<Object> temp = new MySet<Object>();

Iterator<MySet<Object>> itr = ps.iterator();

for (int i=0; i<n;i++) {
MySet<Object> t = new MySet<Object>(itr.next());
}

return ps;
}```

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Returning a set of objects for powerset recursion