For my class, I have been tasked with writing a program in Java that tells you whether 2 DNA sequences each of length 8, stored in an array, are the same. However, the sequences can be cyclic. For example, these two are the same:
A T G C G T A T
A T A T G C G T
I have written code that checks if both arrays at index 0 are the same, and if they are it goes to a check array method, and if they are not it adds 1 to the index of the second array and starts again. However I am pretty stumped as I am not sure how I can input the new index at which the check method go through the two arrays, or how to loop the indexes (i.e. from 7 back to 0).
Sorry if the code is rubbish, I am a beginner and have found this question very confusing. Thanks in advance :)
/* checking whether the two arrays are equal at a certain index
for (x=0;x<8;) {
for (y=0;y<8;) {
if (DNAarray1[x] == DNAarray2[y]) {
isEqual(ADNarray1, ADNarray2);
} else y ;
}
}
/* isEqual method - my issue is with how I can take x and y from above and carry them into this method.
And also how to loop this back round so the index of y goes from 7 back to 0.
static boolean isEqual(int[] ADN1, int[] ADN2) {
for (int c = 1; c < 8; c ) {
if (ADN1[x c] == ADN2[y c]) {
return true;
} else return false;
}
}
CodePudding user response:
To get x and y into the method just add parameters of same name To go back to 0 after 7 use the modulo operator %.
Here is you code with the necessary changes and some fixes:
static boolean isCyclicEqual(int[] DNA1, int[] DNA2) {
for (x=0;x<8;x ) {
for (y=0;y<8;y ) {
if (isEqual(DNAarray1, x, DNAarray2, y)) {
return true;
}
}
}
return false;
}
static boolean isEqual(int[] DNA1, int x, int[] DNA2, int y) {
for (int c = 0; c < 8; c ) {
if (DNA1[(x c) % 8] != DNA2[(y c) % 8]) {
return false;
}
}
return true;
}
CodePudding user response:
In case you have only 8
elements in the arrays, then performance is not critical for the solution. Then it could be like this:
public static boolean isEqualCycle(char[] one, char[] two) {
String strOne = new String(one);
String strTwo = new String(two);
for (int i = 0; i < one.length; i ) {
if (strOne.equalsIgnoreCase(strTwo))
return true;
strTwo = strTwo.charAt(strTwo.length() - 1) strTwo.substring(0, strTwo.length() - 1);
}
return false;
}
Another soulion is to use additional array:
public static boolean isEqualCycle(char[] one, char[] two) {
char[] tmp = Arrays.copyOf(two, two.length);
for (int i = 0; i < one.length - 1; i ) {
if (isEqual(one, tmp))
return true;
rotateRight(tmp);
}
return false;
}
private static boolean isEqual(char[] one, char[] tmp) {
for (int i = 0; i < one.length; i )
if (one[i] != tmp[i])
return false;
return true;
}
private static void rotateRight(char[] arr) {
for (int i = 0, j = arr.length - 1; i < j; i , j--)
swap(arr, i, j);
for (int i = 1, j = arr.length - 1; i < j; i , j--)
swap(arr, i, j);
}
private static void swap(char[] arr, int i, int j) {
char tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
CodePudding user response:
If the DNA sequences are represented as strings / string arrays, it may be convenient to use Collections.rotate
method to perform cyclic shift:
static boolean isEqual(String[] one, String[] two) {
if (one.length != two.length) return false; // verify the length
List<String> sec = Arrays.asList(two);
for (int i = 0; i < one.length; i ) {
if (Arrays.equals(one, two)) {
System.out.println("equal at position=" i);
return true;
}
Collections.rotate(sec, -1); // contents of `two` is rotated as well
}
System.out.println("non-equal");
return false;
}
Test:
isEqual("ATGCGTAT".split(""), "ATATGCGT".split(""));
isEqual("ATGCGTAT".split(""), "TATGGCTA".split(""));
Output:
equal at position=2
non-equal