DNA Sequencing Solution

In case anyone is struggling with this exercise, I have a variant solution to it. Basically, I used a boolean for the if and else statement to mean that if it contains ATG and TGA and their differences in their index is divisible by 3 with no remainder, then it is a protein. The hints in the site are all types of confusing. I just simplified it.

public class DNA {

public static void main(String[] args) {
	String dna1 = "ATGCGATACGCTTGA";
	String dna2 = "ATGCGATACGTGA";
	String dna3 = "ATTAATATGTACTGA";
	
	String dna = dna1;
	
	dna.length();
	
	int ATG = dna.indexOf("ATG");
	int TGA = dna.indexOf("TGA");
	
	int sequenceDifference = ATG - TGA;
	
	if (dna.contains("ATG") && dna.contains("TGA") && (sequenceDifference % 3) == 0) {
		System.out.println("Does contain protein!");
	} else {
		System.out.println("Does not contain protein!");
	}
}

}

5 Likes

Thank you for this!
The Hints confused me more than helped, but your code was clear and explained the entire process for me.

Thank you for this. Clear, simple, and easy to follow. The “steps” in the exercise are super confusing. Codecademy should really take another look at this mess.

Here’s another take on this exercise, if folks are interested:

public class DNA {

  public static void main(String[] args) {

    String dna1 = "ATGCGATACGCTTGA";
    String dna2 = "ATGCGATACGTGA";
    String dna3 = "ATTAATATGTACTGA";
    
    // declares a DNA array
    String[] dnaArray = {dna1, dna2, dna3};
    
    //checks if the DNA strings contain valid proteins
    for (int i = 0; i < dnaArray.length; i++) {
      if (dnaArray[i].contains("ATG") && dnaArray[i].contains("TGA") && dnaArray[i].length() % 3 == 0) {
        System.out.println("This is a valid protein.");
      }
      else {
        System.out.println("This is not a valid protein");
      } 
    }
  }
}

My solution to this exercise.