Skip to content

Commit 5fa4fc0

Browse files
add idiomatic solution
1 parent 4f3c4e6 commit 5fa4fc0

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

docs/src/rosalind/06-hamm.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,28 @@
2727

2828
To calculate the Hamming Distance between two strings/sequences, the two strings/DNA sequences must be the same length.
2929

30-
We can calculate the Hamming Distance by looping over the characters in one of the strings and checking if the corresponding character at the same index in the other string matches.
30+
The simplest way to solve this problem is to compare the corresponding values in each string for each index and then sum the mismatches. This is the fastest and most idiomatic Julia solution, as it leverages vector math.
31+
32+
Let's give this a try!
33+
34+
```julia
35+
ex_seq_a = "GAGCCTACTAACGGGAT"
36+
ex_seq_b = "CATCGTAATGACGGCCT"
37+
38+
count(i-> ex_seq_a[i] != ex_seq_b[i], eachindex(ex_seq_a))
39+
```
40+
41+
42+
43+
### For Loop
44+
45+
Another way we can approach this would be to use the for-loop. This method will be a bit slower.
46+
47+
We can calculate the Hamming Distance by looping over the characters in one of the strings and checking if the corresponding character at the same index in the other string matches.
3148

3249
Each mismatch will cause 1 to be added to a `counter` variable. At the end of the loop, we can return the total value of the `counter` variable.
3350

34-
Let's give this a try!
51+
3552

3653
```julia
3754
ex_seq_a = "GAGCCTACTAACGGGAT"

0 commit comments

Comments
 (0)