-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bec29b1
commit 23a86de
Showing
8 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ build | |
.settings/ | ||
.project | ||
.classpath | ||
|
||
tests/**/*/analysis.json | ||
tests/**/*/tags.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"authors": [ | ||
"wdjunaidi" | ||
], | ||
"contributors": [ | ||
"c-thornton", | ||
"ChristianWilkie", | ||
"FridaTveit", | ||
"javaeeeee", | ||
"jmrunkle", | ||
"jonnynabors", | ||
"jtigger", | ||
"kytrinyx", | ||
"lemoncurry", | ||
"matthewmorgan", | ||
"michael-berger-FR", | ||
"michaelspets", | ||
"mirkoperillo", | ||
"msomji", | ||
"muzimuzhi", | ||
"odzeno", | ||
"sjwarner-bp", | ||
"SleeplessByte", | ||
"Smarticles101", | ||
"sshine", | ||
"stkent", | ||
"t0dd", | ||
"Valkryst", | ||
"vasouv", | ||
"Zaldrick" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/main/java/Hamming.java" | ||
], | ||
"test": [ | ||
"src/test/java/HammingTest.java" | ||
], | ||
"example": [ | ||
".meta/src/reference/java/Hamming.java" | ||
], | ||
"invalidator": [ | ||
"build.gradle" | ||
] | ||
}, | ||
"blurb": "Calculate the Hamming difference between two DNA strands.", | ||
"source": "The Calculating Point Mutations problem at Rosalind", | ||
"source_url": "https://rosalind.info/problems/hamm/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.function.IntPredicate; | ||
import java.util.stream.IntStream; | ||
|
||
class Hamming { | ||
private final int hammingDistance; | ||
|
||
Hamming(String leftStrand, String rightStrand) { | ||
String exceptionMessage = "leftStrand and rightStrand must be of equal length."; | ||
if (leftStrand.length() != rightStrand.length()) { | ||
if (leftStrand.isEmpty()) { | ||
exceptionMessage = "left strand must not be empty."; | ||
} | ||
if (rightStrand.isEmpty()) { | ||
exceptionMessage = "right strand must not be empty."; | ||
} | ||
throw new IllegalArgumentException(exceptionMessage); | ||
} | ||
|
||
IntPredicate areNotEqual = index -> leftStrand.charAt(index) != rightStrand.charAt(index); | ||
hammingDistance = (int) IntStream.range(0, leftStrand.length()).filter(areNotEqual).count(); | ||
} | ||
|
||
int getHammingDistance() { | ||
return hammingDistance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[f6dcb64f-03b0-4b60-81b1-3c9dbf47e887] | ||
description = "empty strands" | ||
|
||
[54681314-eee2-439a-9db0-b0636c656156] | ||
description = "single letter identical strands" | ||
|
||
[294479a3-a4c8-478f-8d63-6209815a827b] | ||
description = "single letter different strands" | ||
|
||
[9aed5f34-5693-4344-9b31-40c692fb5592] | ||
description = "long identical strands" | ||
|
||
[cd2273a5-c576-46c8-a52b-dee251c3e6e5] | ||
description = "long different strands" | ||
|
||
[919f8ef0-b767-4d1b-8516-6379d07fcb28] | ||
description = "disallow first strand longer" | ||
|
||
[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e] | ||
description = "disallow second strand longer" | ||
|
||
[5dce058b-28d4-4ca7-aa64-adfe4e17784c] | ||
description = "disallow left empty strand" | ||
|
||
[38826d4b-16fb-4639-ac3e-ba027dec8b5f] | ||
description = "disallow right empty strand" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"comments":["java.hamming.calculate_distance_in_constructor","java.hamming.should_use_string_is_empty"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
class Hamming { | ||
|
||
private String leftStrand; | ||
private String rightStrand; | ||
|
||
Hamming(String leftStrand, String rightStrand) { | ||
|
||
validate(leftStrand, rightStrand); | ||
this.leftStrand = leftStrand; | ||
this.rightStrand = rightStrand; | ||
} | ||
|
||
|
||
private void validate(String leftStrand, String rightStrand) { | ||
validateNotNull(leftStrand, rightStrand); | ||
validateNotOneEmpty(leftStrand, rightStrand); | ||
validateSameLength(leftStrand, rightStrand); | ||
} | ||
|
||
private void validateNotNull(String leftStrand, String rightStrand) { | ||
if (leftStrand == null || rightStrand == null) { | ||
throw new IllegalArgumentException("Either left or right stand is null"); | ||
} | ||
} | ||
|
||
private void validateNotOneEmpty(String leftStrand, String rightStrand) { | ||
if (leftStrand.length() == 0 && rightStrand.length() > 0) { | ||
throw new IllegalArgumentException("left strand must not be empty."); | ||
} else if (rightStrand.length() == 0 && leftStrand.length() > 0) { | ||
throw new IllegalArgumentException("right strand must not be empty."); | ||
} | ||
} | ||
|
||
private void validateSameLength(String leftStrand, String rightStrand) { | ||
if (leftStrand.length() != rightStrand.length()) { | ||
throw new IllegalArgumentException("leftStrand and rightStrand must be of equal length."); | ||
} | ||
} | ||
|
||
int getHammingDistance() { | ||
|
||
int strandLen = this.leftStrand.length(); | ||
int diffCount = 0; | ||
for (int i = 0; i < strandLen; i++) { | ||
if (this.leftStrand.charAt(i) != this.rightStrand.charAt(i)) { | ||
diffCount++; | ||
} | ||
} | ||
|
||
return diffCount; | ||
} | ||
|
||
} |