Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The Hamming distance between two strings of equal length is the number of positions
- * at which the corresponding symbols are different. In other words, it measures the
- * minimum number of substitutions required to change one string into the other.
- *
- * @author jdalbey
- * @version 0.1
- */
- public class Hamming
- {
- /**
- * Return the hamming distance between two strings.
- *
- * @param alpha a string
- * @param bravo a string
- * @pre alpha.length() == bravo.length();
- * @return number of positions at which corresponding characters differ
- */
- public static int getDistance(String alpha, String bravo)
- {
- int count = 0;
- // Iterate through all characters in both strings
- for (int pos = 0; pos < alpha.length(); pos++)
- {
- // If they don't match, increment the counter
- if (alpha.charAt(pos) != bravo.charAt(pos))
- {
- count++;
- }
- }
- return count;
- }
- }
- /**
- * The test class HammingDistanceTest.
- *
- * @author jdalbey
- * @version 0.1
- */
- public class HammingDistanceTest extends junit.framework.TestCase
- {
- public void testOne()
- {
- assertEquals(0, Hamming.getDistance("abc", "abc"));
- assertEquals(3, Hamming.getDistance("aaa", "bbb"));
- assertEquals(1, Hamming.getDistance("baa", "aaa"));
- assertEquals(1, Hamming.getDistance("aba", "aaa"));
- assertEquals(1, Hamming.getDistance("aab", "aaa"));
- assertEquals(2, Hamming.getDistance("bab", "aaa"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement