ClassicComputerScienceProblemsInJava
ClassicComputerScienceProblemsInJava copied to clipboard
Error in constructor of Gene class -- Listing 2.4 on page 29
trafficstars
Listing 2.4 on page 29:
// BUG: this code contains an error -- fails to read last three characters of string
for (int i = 0; i < geneStr.length() - 3; i += 3) {
codons.add(new Codon(geneStr.substring(i, i + 3)));
}
// Here is an example of a corrected version:
final int len = geneStr.length();
final int partitionSize = 3;
for (int i = 0; i < len; i += partitionSize) {
codons.add(new Codon(geneStr.substring(i, Math.min(len, i + partitionSize))));
}
// Test case that fails with original implementation
Codon ttt = new Codon("TTT");
System.out.println(myGene.linearContains(ttt)); // true
System.out.println(myGene.binaryContains(ttt)); // true
Note: I would recommend to only update this code on GitHub, as clearly this is being used to read in sample input data for the algorithm.