Skip to content

Commit

Permalink
Add exercise: nucleotide count (JuliaLang#31)
Browse files Browse the repository at this point in the history
* Add exercise: nucleotide count

* Remove hard-coded tuple of valid symbols
  • Loading branch information
SaschaMann authored Feb 8, 2017
1 parent 7ba73f2 commit b19b198
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
"generators"
]
},
{
"slug": "nucleotide-count",
"difficulty": 1,
"topics": [
"chars",
"strings",
"control-flow (loops)"
]
},
{
"slug": "word-count",
"difficulty": 1,
Expand Down
8 changes: 8 additions & 0 deletions exercises/nucleotide-count/example.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function count_nucleotides(strand::AbstractString)
counter = Dict('A' => 0, 'C' => 0, 'G' => 0, 'T' => 0)
for sym in strand
sym in keys(counter) || error("Invalid nucleotide in strand")
counter[sym] += 1
end
return counter
end
3 changes: 3 additions & 0 deletions exercises/nucleotide-count/nucleotide-count.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function count_nucleotides(strand::AbstractString)

end
19 changes: 19 additions & 0 deletions exercises/nucleotide-count/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Base.Test

include("nucleotide-count.jl")

@testset "empty strand" begin
@test count_nucleotides("") == Dict('A' => 0, 'C' => 0, 'G' => 0, 'T' => 0)
end

@testset "strand with repeated nucleotide" begin
@test count_nucleotides("GGGGGGG") == Dict('A' => 0, 'C' => 0, 'G' => 7, 'T' => 0)
end

@testset "strand with multiple nucleotides" begin
@test count_nucleotides("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC") == Dict('A' => 20, 'C' => 12, 'G' => 17, 'T' => 21)
end

@testset "strand with invalid nucleotides" begin
@test_throws ErrorException count_nucleotides("AGXXACT")
end

0 comments on commit b19b198

Please sign in to comment.