forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add exercise: hamming (JuliaLang#12)
- Loading branch information
1 parent
87bfac6
commit 85b0b3b
Showing
4 changed files
with
59 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
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,5 @@ | ||
function distance(s1::AbstractString, s2::AbstractString) | ||
length(s1) != length(s2) && throw(ArgumentError("Sequences must have the same length")) | ||
|
||
reduce(+, 0, a != b for (a, b) in zip(s1, s2)) | ||
end |
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,3 @@ | ||
function distance(s1::AbstractString, s2::AbstractString) | ||
|
||
end |
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,42 @@ | ||
using Base.Test | ||
|
||
include("hamming.jl") | ||
|
||
@testset "identical strands" begin | ||
@test distance("A", "A") == 0 | ||
@test distance("GGACTGA", "GGACTGA") == 0 | ||
end | ||
|
||
@testset "complete distance" begin | ||
@test distance("A", "G") == 1 | ||
@test distance("AG", "CT") == 2 | ||
end | ||
|
||
@testset "small distance" begin | ||
@test distance("AT", "CT") == 1 | ||
@test distance("GGACG", "GGTCG") == 1 | ||
@test distance("ACCAGGG", "ACTATGG") == 2 | ||
end | ||
|
||
@testset "non-unique characters" begin | ||
@test distance("AGA", "AGG") == 1 | ||
@test distance("AGG", "AGA") == 1 | ||
end | ||
|
||
@testset "same nucleotides in different positions" begin | ||
@test distance("TAG", "GAT") == 2 | ||
end | ||
|
||
@testset "large distance" begin | ||
@test distance("GATACA", "GCATAA") == 4 | ||
@test distance("GGACGGATTCTG", "AGGACGGATTCT") == 9 | ||
end | ||
|
||
@testset "empty strands" begin | ||
@test distance("", "") == 0 | ||
end | ||
|
||
@testset "different strand lengths" begin | ||
@test_throws ArgumentError distance("AATG", "AAA") | ||
@test_throws ArgumentError distance("ATA", "AGTG") | ||
end |