Skip to content

Commit d75f834

Browse files
committed
add week2 solution
1 parent c32ecf3 commit d75f834

File tree

11 files changed

+710
-0
lines changed

11 files changed

+710
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Finding a Gene - Using the Simplified Algorithm
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
public class Part1 {
9+
public String findSimpleGene(String dna) {
10+
String result = "";
11+
12+
int startIndex = dna.indexOf("ATG");
13+
int stopIndex = dna.indexOf("TAA", startIndex + 3);
14+
15+
if (startIndex == -1 || stopIndex == -1) {
16+
return result;
17+
}
18+
19+
if ((stopIndex - startIndex) % 3 == 0) {
20+
result = dna.substring(startIndex, stopIndex + 3);
21+
}
22+
23+
return result;
24+
}
25+
26+
public void testSimpleGene() {
27+
// DNA with no ATG
28+
String dna = "ATCTAACATC";
29+
System.out.println("DNA strand is " + dna);
30+
System.out.println("Gene is " + findSimpleGene(dna));
31+
32+
// DNA with no TAA
33+
dna = "ATTATCATGTTA";
34+
System.out.println("DNA strand is " + dna);
35+
System.out.println("Gene is " + findSimpleGene(dna));
36+
37+
// DNA with no “ATG” and “TAA”
38+
dna = "ATTAGTGTA";
39+
System.out.println("DNA strand is " + dna);
40+
System.out.println("Gene is " + findSimpleGene(dna));
41+
42+
// DNA with ATG, TAA and the substring between them is not a multiple of 3
43+
dna = "GAAATGGATAGTAA";
44+
System.out.println("DNA strand is " + dna);
45+
System.out.println("Gene is " + findSimpleGene(dna));
46+
47+
// DNA with ATG, TAA and the substring between them is a multiple of 3 (a gene)
48+
dna = "TAAGATATGGTGAAGTAA";
49+
System.out.println("DNA strand is " + dna);
50+
System.out.println("Gene is " + findSimpleGene(dna));
51+
}
52+
}
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
/**
3+
* Finding a Gene - Using the Simplified Algorithm Reorganized
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
public class Part2 {
9+
public String findSimpleGene(String dna, String startCodon, String stopCodon) {
10+
String result = "";
11+
12+
int startIndex = dna.toUpperCase().indexOf(startCodon);
13+
int stopIndex = dna.toUpperCase().indexOf(stopCodon, startIndex + 3);
14+
15+
if (startIndex == -1 || stopIndex == -1) {
16+
return result;
17+
}
18+
19+
if ((stopIndex - startIndex) % 3 == 0) {
20+
result = dna.substring(startIndex, stopIndex + 3);
21+
}
22+
23+
return result;
24+
}
25+
26+
public void testSimpleGene() {
27+
String startCodon = "ATG";
28+
String stopCodon = "TAA";
29+
30+
// DNA with no ATG
31+
String dna = "ATCTAACATC";
32+
System.out.println("DNA strand is " + dna);
33+
System.out.println("Gene is " + findSimpleGene(dna, startCodon,stopCodon));
34+
35+
// DNA with no TAA
36+
dna = "ATTATCATGTTA";
37+
System.out.println("DNA strand is " + dna);
38+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
39+
40+
// DNA with no “ATG” and “TAA”
41+
dna = "ATTAGTGTA";
42+
System.out.println("DNA strand is " + dna);
43+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
44+
45+
// DNA with ATG, TAA and the substring between them is not a multiple of 3
46+
dna = "GAAATGGATAGTAA";
47+
System.out.println("DNA strand is " + dna);
48+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
49+
50+
// DNA with ATG, TAA and the substring between them is a multiple of 3 (a gene)
51+
dna = "TAAGATATGGTGAAGTAA";
52+
System.out.println("DNA strand is " + dna);
53+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
54+
55+
dna = "ATGGGTTAAGTC";
56+
System.out.println("DNA strand is " + dna);
57+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
58+
59+
dna = "gatgctataat";
60+
System.out.println("DNA strand is " + dna);
61+
System.out.println("Gene is " + findSimpleGene(dna, startCodon, stopCodon));
62+
}
63+
}
64+
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
/**
3+
* Problem Solving with Strings
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
public class Part3 {
9+
public boolean twoOccurrences(String stringa, String stringb) {
10+
int firstIndex = stringb.indexOf(stringa);
11+
int lastIndex = stringb.lastIndexOf(stringa);
12+
13+
if (firstIndex != lastIndex) {
14+
return true;
15+
} else {
16+
return false;
17+
}
18+
}
19+
20+
public String lastPart(String stringa, String stringb) {
21+
int index = stringb.indexOf(stringa);
22+
23+
if (index == -1) {
24+
return stringb;
25+
} else {
26+
return stringb.substring(index + stringa.length());
27+
}
28+
}
29+
30+
public void test() {
31+
String stringa;
32+
String stringb;
33+
34+
System.out.println("Testing twoOccurences");
35+
System.out.println("=====================");
36+
37+
stringa = "by";
38+
stringb = "A story by Abby Long";
39+
System.out.println(stringa + " appears at least twice in " + stringb + " = " + twoOccurrences(stringa, stringb));
40+
41+
stringa = "a";
42+
stringb = "banana";
43+
System.out.println(stringa + " appears at least twice in " + stringb + " = " + twoOccurrences(stringa, stringb));
44+
45+
stringa = "atg";
46+
stringb = "ctgtatgta";
47+
System.out.println(stringa + " appears at least twice in " + stringb + " = " + twoOccurrences(stringa, stringb));
48+
49+
System.out.println("\n\nTesting lastPart:");
50+
System.out.println("=================");
51+
52+
stringa = "an";
53+
stringb = "banana";
54+
System.out.println("The part of the string after " + stringa + " in " + stringb + " is " + lastPart(stringa, stringb));
55+
56+
stringa = "zoo";
57+
stringb = "forest";
58+
System.out.println("The part of the string after " + stringa + " in " + stringb + " is " + lastPart(stringa, stringb));
59+
}
60+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/**
3+
* Finding Web Links
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
9+
import edu.duke.*;
10+
11+
public class Part4 {
12+
public void findLinks(String url) {
13+
URLResource urlResource = new URLResource(url);
14+
15+
for (String line : urlResource.lines()) {
16+
int youtubeIndex = line.toLowerCase().indexOf("youtube.com");
17+
18+
if (youtubeIndex != -1) {
19+
int startIndex = line.lastIndexOf("\"", youtubeIndex);
20+
int lastIndex = line.indexOf("\"", youtubeIndex);
21+
22+
System.out.println("Youtube link: " + line.substring(startIndex + 1, lastIndex));
23+
}
24+
}
25+
}
26+
27+
public void test() {
28+
String url = "http://www.dukelearntoprogram.com/course2/data/manylinks.html";
29+
findLinks(url);
30+
}
31+
32+
}
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
/**
3+
* Finding many Genes
4+
*
5+
* @author Deny Kiantono
6+
* @version 1
7+
*/
8+
public class Part1 {
9+
public int findStopCodon(String dna, int startIndex, String stopCodon) {
10+
int index = 0;
11+
12+
while(true) {
13+
index = dna.indexOf(stopCodon, startIndex + 3);
14+
15+
if (index == -1 || (index - startIndex) % 3 == 0) {
16+
break;
17+
}
18+
19+
startIndex += 3;
20+
}
21+
22+
if (index != -1) {
23+
return index;
24+
} else {
25+
return dna.length();
26+
}
27+
}
28+
29+
public void testFindStopCodon() {
30+
String dna = "xxxyyyzzzTAAxxxyyyzzzTAAxx";
31+
32+
int index = findStopCodon(dna, 0, "TAA");
33+
System.out.println("Index = " + index);
34+
35+
index = findStopCodon(dna, 9, "TAA");
36+
System.out.println("Index = " + index);
37+
38+
index = findStopCodon(dna, 1, "TAA");
39+
System.out.println("Index = " + index);
40+
41+
index = findStopCodon(dna, 0, "TAG");
42+
System.out.println("Index = " + index);
43+
}
44+
45+
public String findGene(String dna, int start) {
46+
final String START_CODON = "ATG";
47+
int startIndex = dna.indexOf(START_CODON, start);
48+
49+
if (startIndex == -1) {
50+
return "";
51+
}
52+
53+
int taaIndex = findStopCodon(dna, startIndex, "TAA");
54+
int tagIndex = findStopCodon(dna, startIndex, "TAG");
55+
int tgaIndex = findStopCodon(dna, startIndex, "TGA");
56+
57+
int minIndex = Math.min(taaIndex, Math.min(tagIndex, tgaIndex));
58+
59+
if (minIndex == dna.length()) {
60+
return "";
61+
} else {
62+
return dna.substring(startIndex, minIndex + 3);
63+
}
64+
}
65+
66+
public void testFindGene() {
67+
String dna = "GTTAATGTAGCTTAAACCTTTAAAGCAAGGCACTGAAAATGCCTAGATGA";
68+
System.out.println("Gene: " + findGene(dna, 0));
69+
70+
dna = "GTGAGCTCACTCCATAGACACAAAGGTTTGGTCCTGGCCTTCTTATTAGT";
71+
System.out.println("Gene: " + findGene(dna, 0));
72+
73+
dna = "TTTCAGTGAGCTTACACATGCAAGTATCCGCGCGCCAGTGAAAATGCCCT";
74+
System.out.println("Gene: " + findGene(dna, 0));
75+
76+
dna = "TCAAATCATTACTGACCATAAAGGAGCGGGTATCAAGCACACACCTATGT";
77+
System.out.println("Gene: " + findGene(dna, 0));
78+
79+
dna = "AGCTCACAACACCTTGCTTAGCCACACCCCCACGGGATACAGCAGTGATA";
80+
System.out.println("Gene: " + findGene(dna, 0));
81+
}
82+
83+
public void printAllGenes(String dna) {
84+
int start = 0;
85+
86+
while (true) {
87+
String gene = findGene(dna, start);
88+
89+
if (gene.isEmpty()) {
90+
break;
91+
}
92+
93+
System.out.println("Gene: " + gene);
94+
95+
start = dna.indexOf(gene, start) + gene.length();
96+
}
97+
}
98+
99+
public void testPrintAllGenes() {
100+
String dna = "ATGATCTAATTTATGCTGCAACGGTGAAGA";
101+
printAllGenes(dna);
102+
}
103+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* HowMany - Finding Multiple Occurrences
4+
*
5+
* @author Deny Kiantono
6+
* @version 1.0
7+
*/
8+
public class Part2 {
9+
public int howMany(String stringa, String stringb) {
10+
int totalOccurences = 0;
11+
int start = 0;
12+
13+
while (true) {
14+
int index = stringb.indexOf(stringa, start);
15+
16+
if (index == -1) {
17+
break;
18+
}
19+
20+
totalOccurences++;
21+
start = index + stringa.length();
22+
}
23+
24+
return totalOccurences;
25+
}
26+
27+
public void testHowMany() {
28+
String stringa = "GAA";
29+
String stringb = "ATGAACGAATTGAATC";
30+
System.out.println("Total occurences of " + stringa + " in " + stringb + " = " + howMany(stringa, stringb));
31+
32+
stringa = "AA";
33+
stringb = "ATAAAA";
34+
System.out.println("Total occurences of " + stringa + " in " + stringb + " = " + howMany(stringa, stringb));
35+
}
36+
}

0 commit comments

Comments
 (0)