Skip to content

Commit

Permalink
Add exercise: hamming (JuliaLang#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaMann authored Jan 28, 2017
1 parent 87bfac6 commit 85b0b3b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
"active": false,
"test_pattern": "TODO",
"exercises": [
{
"slug": "hamming",
"difficulty": 1,
"topics": [
"generators",
"strings",
"exceptions"
]
},
{
"slug": "difference-of-squares",
"difficulty": 1,
Expand Down
5 changes: 5 additions & 0 deletions exercises/hamming/example.jl
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
3 changes: 3 additions & 0 deletions exercises/hamming/hamming.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function distance(s1::AbstractString, s2::AbstractString)

end
42 changes: 42 additions & 0 deletions exercises/hamming/runtests.jl
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

0 comments on commit 85b0b3b

Please sign in to comment.