permutation function problem
- From: tiger66 <liuallen1981@xxxxxxxxx>
- Date: Fri, 29 Jun 2007 12:19:26 -0700
Hi All
I am trying to create a permutation function. I found the
implementation online, but the implementation uses for() loop in the
main() and it has the List as a parameter. All I am trying to do is to
modify the implementation, so that all the loops are in the function
and the parameter is a char array instead of a list.
The online implementations prints all the entry:
i.e
1.
0: ABC
2.
1: ACB
3.
2: BAC
4.
3: BCA
5.
4: CAB
6.
5: CBA
However, my function can only print ABC. I can't figure out where I
did wrong.
Could someone please help.. I would really appreciate it
Here is the online implementation:
public static <T> List<T> permute(List<T> list, int r) {
int n= list.size();
int f= fac(n);
: List<T> perm= new ArrayList<T>();
list= new ArrayList<T>(list);
for (list= new ArrayList<T>(list); n > 0; n--, r%= f) {
f/= n;
perm.add(list.remove(r/f));
}
return perm;
}
public static void main(String[] args) {
List<String> list= Arrays.asList("A", "B", "C");
for (int i= 0, n= 3, f= fac(n); i < f; i++)
System.out.println(i+": "+perm(list, i));
}
}
Here is my implementation(not working)
public static List permute(char [] input) {
List list = new ArrayList();
for(int count=0; count<input.length; count++){
list.add(input[count]);
}
list = new ArrayList(list);
List perm= new ArrayList();
for(int r=0, n=3, f=6; r<f; r++){
for (list= new ArrayList(list); n > 0; n--, r%=f) {
f/= n;
perm.add(list.remove(r/f));
}
System.out.print("r = " + r);
System.out.println(perm);
}
System.out.print("permSize = " + perm.size());
return perm;
}
public static void main(String[] args) {
char array[] = {'a', 'b', 'c'};
List list = new ArrayList();
list = permute(array);
System.exit(0);
}
.
- Follow-Ups:
- Re: permutation function problem
- From: Wade Ward
- Re: permutation function problem
- Prev by Date: Re: Question of the software develpment cycle
- Next by Date: Re: Application programming language of choice
- Previous by thread: linking error :undefined reference to a function
- Next by thread: Re: permutation function problem
- Index(es):
Relevant Pages
|