Use for loop to get Hamming distance between two lines

In this problem, I need to get the Hamming distance (the Hamming distance between two lines of equal length is the number of positions at which the corresponding characters differ - from Wikipedia) between two sequences of sequence 1 and sequence2.

At first, I made 2 new lines, which are 2 source lines, but with a reduced case, to simplify the comparison. Then I resorted to using a for loop and if we compare 2 lines. For any differences in characters in these two pairs of lines, the loop will add 1 to int x = 0. The method return will be the value of this x.

public static int getHammingDistance(String sequence1, String sequence2) { int a = 0; String sequenceX = sequence1.toLowerCase(); String sequenceY = sequence2.toLowerCase(); for (int x = 0; x < sequenceX.length(); x++) { for (int y = 0; y < sequenceY.length(); y++) { if (sequenceX.charAt(x) == sequenceY.charAt(y)) { a += 0; } else if (sequenceX.charAt(x) != sequenceY.charAt(y)) { a += 1; } } } return a; } 

So the code looks good and quite functional? Anything I could fix or optimize the code? Thank you in advance. I'm a huge noob, so forgive me if I ask for something stupid

+6
source share
4 answers

From my point of view, the following implementation will be fine:

 public static int getHammingDistance(String sequence1, String sequence2) { char[] s1 = sequence1.toCharArray(); char[] s2 = sequence2.toCharArray(); int shorter = Math.min(s1.length, s2.length); int longest = Math.max(s1.length, s2.length); int result = 0; for (int i=0; i<shorter; i++) { if (s1[i] != s2[i]) result++; } result += longest - shorter; return result; } 
  • uses an array, which avoids calling two methods (charAt) for each individual char that needs to be compared;
  • avoid exceptions when one line is longer than the other.
+5
source

your code is completely disabled. as you said yourself, the distance is the number of places where the lines differ - so you should have only 1 cycle, moving along both strings at once. instead, you have 2 nested loops that compare each index on row a with each index on row b.

also writing down the if condition, which leads to a+=0 , is a waste of time.

try this instead:

 for (int x = 0; x < sequenceX.length(); x++) { //both are of the same length if (sequenceX.charAt(x) != sequenceY.charAt(x)) { a += 1; } } 

In addition, this is still a naive approach that will not work with complex Unicode characters (where 2 characters can be logically equal, but do not have the same character code)

+3
source
 public static int getHammingDistance(String sequenceX, String sequenceY) { int a = 0; // String sequenceX = sequence1.toLowerCase(); //String sequenceY = sequence2.toLowerCase(); if (sequenceX.length() != sequenceY.length()) { return -1; //input strings should be of equal length } for (int i = 0; i < sequenceX.length(); i++) { if (sequenceX.charAt(i) != sequenceY.charAt(i)) { a++; } } return a; } 
+1
source

Your code is fine, however I offer you the following improvements.

  • do not use charAt() strings. Get the char array from a string using toCharArray() before the loop, and then work with that array. It is more readable and more efficient.
  • Structure

      if (sequenceX.charAt(x) == sequenceY.charAt(y)) { a += 0; } else if (sequenceX.charAt(x) != sequenceY.charAt(y)) { a += 1; } 

    looks redundant. Correct it: if (sequenceX.charAt (x) == sequenceY.charAt (y)) {a + = 0; } else {a + = 1; }

Also, given that I recommended you work with an array, change it to something like:

a += seqx[x] == seqY[x] ? 0 : 1

fewer code errors less ...

EDIT: as @radai mentioned, you don't need an if/else structure: adding 0 to a is redundant.

0
source

Source: https://habr.com/ru/post/943800/


All Articles