Skip to content

Instantly share code, notes, and snippets.

@smnatale
Created June 4, 2020 22:03
Show Gist options
  • Select an option

  • Save smnatale/eca3993bef225fc7a0648958fbdb418c to your computer and use it in GitHub Desktop.

Select an option

Save smnatale/eca3993bef225fc7a0648958fbdb418c to your computer and use it in GitHub Desktop.
Codecademy Challenge - Program checks if a DNA strand contains a protein
// Program is designed to check if a DNA Sequence is a protein, it does this by checking if it contains ATG, contains TGA at some point after ATG and the characters inbetween are divisible by 3
public class DNA{
public boolean isProtein(String dna){
// The begins and ends variables find the index value of where ATG starts and the index value of where TGA starts + 3 so we get the value of where it ends
int begins = dna.indexOf("ATG");
int ends = (dna.indexOf("TGA")) + 3;
// The string newDna now will hold the new string starting with ATG and ending with TGA cutting out anything before or after in the original string
String newDna = dna.substring(begins,ends);
int len = newDna.length();
// The if statement checks if the whole string is divisible by 3 as if this is incorrect then the DNA sequence is not a protein
if ((len % 3 ==0) && (begins != -1) && (ends !=-1)) {
return true;
}
return false;
}
// Here the strings are defined and isProtein is called to check each one, the values are then printed at the end
public static void main(String[] args){
String dnaA = "ATGCGATACGCTTGA";
String dnaB = "ATGCGATACGTGA";
String dnaC = "ATTAATATGTACTGA";
DNA dna = new DNA();
boolean A = dna.isProtein(dnaA);
boolean B = dna.isProtein(dnaB);
boolean C = dna.isProtein(dnaC);
System.out.println("The DNA strand '" + dnaA + "' contains a protein: " + A);
System.out.println("The DNA strand '"+ dnaB +"' contains a protein: " + B);
System.out.println("The DNA strand '"+ dnaC +"' contains a protein: " + C);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment