Below is my solution for the DNA homework. There are other ways to solve this problem.
import java.util.Scanner; public class DNASearch { static final int MAX_ERRORS = 1; public static void main(String[] args){ String dna = "ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC" + "CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGC" + "CTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGG" + "AAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCC" + "CTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAG" + "TTTAATTACAGACCTGAA"; Scanner keyboard = new Scanner(System.in); System.out.print("Enter query:"); String query = keyboard.next(); for (int dnaIndex =0; dnaIndex < dna.length(); dnaIndex++){ //start comparing at every index int numMatches = 0; //calculate the number of matches between dna[dnaIndex to dnaIndex+query.lenth()] and query for (int queryIndex = 0; queryIndex < query.length() && queryIndex + dnaIndex < dna.length(); queryIndex++){ if (dna.charAt(dnaIndex + queryIndex) == query.charAt(queryIndex)) numMatches++; } if (numMatches >= query.length() - MAX_ERRORS){ System.out.println("Match at position " + dnaIndex); } } } }
No comments:
Post a Comment