tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
25
Task/Power-set/Java/power-set-1.java
Normal file
25
Task/Power-set/Java/power-set-1.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
public static ArrayList<String> getpowerset(int a[],int n,ArrayList<String> ps)
|
||||
{
|
||||
if(n<0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(n==0)
|
||||
{
|
||||
if(ps==null)
|
||||
ps=new ArrayList();
|
||||
ps.add(" ");
|
||||
return ps;
|
||||
}
|
||||
ps=getpowerset(a, n-1, ps);
|
||||
ArrayList<String> tmp=new ArrayList<String>();
|
||||
for(String s:ps)
|
||||
{
|
||||
if(s.equals(" "))
|
||||
tmp.add(""+a[n-1]);
|
||||
else
|
||||
tmp.add(s+a[n-1]);
|
||||
}
|
||||
ps.addAll(tmp);
|
||||
return ps;
|
||||
}
|
||||
23
Task/Power-set/Java/power-set-2.java
Normal file
23
Task/Power-set/Java/power-set-2.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
public static <T> List<List<T>> powerset(Collection<T> list) {
|
||||
List<List<T>> ps = new ArrayList<List<T>>();
|
||||
ps.add(new ArrayList<T>()); // add the empty set
|
||||
|
||||
// for every item in the original list
|
||||
for (T item : list) {
|
||||
List<List<T>> newPs = new ArrayList<List<T>>();
|
||||
|
||||
for (List<T> subset : ps) {
|
||||
// copy all of the current powerset's subsets
|
||||
newPs.add(subset);
|
||||
|
||||
// plus the subsets appended with the current item
|
||||
List<T> newSubset = new ArrayList<T>(subset);
|
||||
newSubset.add(item);
|
||||
newPs.add(newSubset);
|
||||
}
|
||||
|
||||
// powerset is now powerset of list.subList(0, list.indexOf(item)+1)
|
||||
ps = newPs;
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
16
Task/Power-set/Java/power-set-3.java
Normal file
16
Task/Power-set/Java/power-set-3.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
public static <T extends Comparable<? super T>> LinkedList<LinkedList<T>> BinPowSet(
|
||||
LinkedList<T> A){
|
||||
LinkedList<LinkedList<T>> ans= new LinkedList<LinkedList<T>>();
|
||||
int ansSize = (int)Math.pow(2, A.size());
|
||||
for(Integer i= 0;i< ansSize;++i){
|
||||
String bin= Integer.toString(i, 2); //convert to binary
|
||||
while(bin.length() < A.size())bin = "0" + bin; //pad with 0's
|
||||
LinkedList<T> thisComb = new LinkedList<T>(); //place to put one combination
|
||||
for(int j= 0;j< A.size();++j){
|
||||
if(bin.charAt(j) == '1')thisComb.add(A.get(j));
|
||||
}
|
||||
Collections.sort(thisComb); //sort it for easy checking
|
||||
ans.add(thisComb); //put this set in the answer list
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue