Created
April 19, 2016 22:26
-
-
Save tarasjg/751840b91f432d13c563896babb0345f to your computer and use it in GitHub Desktop.
recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Genes { | |
public static void main(String[] args) { | |
Scanner stdin = new Scanner(System.in); | |
String gene = stdin.next(); | |
String result = findGene(gene, ""); | |
if (!result.isEmpty()) { | |
System.out.println(result); | |
} else { | |
System.out.println("No valid genes were detected."); | |
} | |
} | |
public static String findGene(String s1, String allGenes) { | |
String start = "ATG"; | |
String[] ends = {"TAG", "TAA", "TGA"}; | |
int starter = s1.indexOf(start); | |
int ender = s1.indexOf(ends[0]); | |
for (int i = 1; i < 3; i++) { | |
if (s1.indexOf(ends[i]) == -1) continue; | |
if ((s1.indexOf(ends[i]) < ender && s1.indexOf(ends[i]) > starter) || ender == -1) { | |
ender = s1.indexOf(ends[i]); | |
} | |
} | |
if (s1.isEmpty() || starter == -1 || ender == -1) return allGenes; | |
if (s1.substring(starter + 3, ender).length() % 3 == 0) { | |
allGenes = s1.substring(starter + 3, ender) + "\n"; | |
} | |
s1 = s1.substring(ender + 3, s1.length()); | |
return allGenes + findGene(s1, ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment