简体   繁体   中英

Need help creating permuation system of amino acids using java

My problem requires me to create a list of all 20 amino acids permutations based on a user imputed chain length. For example, I currently have code that works if the user wants a 3 chain length. This is 20^3 possibilities. So I have 3 nested for loops that run through all possibilities, then a counter that outputs the number of permutations to make sure the answer is correct. How could I code this method so it output permutations based on user input?

protected void getPermutations(int chainlength) {
    int counter = 0;
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            for (int k = 0; k < 20; k++) {
                System.out.println(AcidArray[i].getTcode() + "-"
                        + AcidArray[j].getTcode() + "-"
                        + AcidArray[k].getTcode());
                counter++;
            }
        }
    }
    System.out.println("chain length = " + chainlength);
    System.out.println(counter + " permutations");
}

Thanks

Recursion is your friend in this situation

protected String getPermutations(int chainlength) {
    int counter = 0;
    if(chainlength > 0) { // so that the counter is not 1
        counter = getSubPermutations("", chainlength));
    }
    System.out.println("chain length = " + chainlength);
    System.out.println(counter + " permutations");
}

private int getSubPermutations(String prefix, int chainlength){
   if(chainlength == 0){ //The bottom of the stack, print out the combination
      System.out.println(prefix.substring(0,prefix.length-1)); //remove the final '-'
      return 1;
   } else {
      int counter = 0
      for(int i = 0; i < 20; i++) {
        //Add this level T code to the string and pass it on
        counter += getSubPermutations(prefix + AcidArray[i].getTcode() + "-", chainlength-1);
      }
      return counter;
   }
}

What this will do is make a tree of calls. If chainlength is one then it will call getSubPermutations with 1 . This will run through the for loop calling getSubPermutations again with the String for the first value and a chainlength of 0 . In this case the string will only have one T code in it. Each inner call will hit the first if statement so it will print out the string containing one T code and return 1 . All these will be added up so the counter returned to getPermutations will be 20. By this stage all the permutations will have been printed out.

As chain length increases getSubPermuations is called recursively. With a chainlength of 2 it will call getSubPermutations 20 times with a chain length of 1 , passing in the string of the T code. Each of these will call getSubPermutations with a chainlength of 0, with a string containing two T codes . This will then print out the full string and return 1 . These return values will get added up to 20 as in the previous example but now when they are returned to the next level they are added up to return a final 400 to getPermutations and 400 Strings will have been printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM