Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.
The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.
Here is an analogy:
To run the tests, run the command busted
from within the exercise directory.
For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.
The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
local DNA = require('nucleotide-count')
describe('nucleotide-count', function()
it('has no nucleotides', function()
local expected = { A = 0, T = 0, C = 0, G = 0 }
dna = DNA:new('')
local result = dna.nucleotideCounts
assert.are.same(expected, result)
end)
it('has no adenosine', function()
local dna = DNA:new('')
local expected = 0
result = dna:count('A')
assert.are.same(expected, result)
end)
it('repetitive cytosine gets counts', function()
local dna = DNA:new('CCCCC')
local expected = 5
result = dna:count('C')
assert.are.same(expected, result)
end)
it('repetitive sequence has only guanosine', function()
local dna = DNA:new('GGGGGGGG')
expected = { A = 0, T = 0, C = 0, G = 8 }
result = dna.nucleotideCounts
assert.are.same(expected, result)
end)
it('counts only thymine', function()
local dna = DNA:new('GGGGTAACCCGG')
expected = 1
result = dna:count('T')
assert.are.same(expected, result)
end)
it('counts a nucleotide only once', function()
local dna = DNA:new('GGTTGG')
expected = 2
result = dna:count('T')
assert.are.same(expected, result)
end)
it('validates nucleotides', function()
local dna = DNA:new('GGTTGG')
assert.has.errors(function() dna:count('X') end)
assert.has_error(function() dna:count('X') end, 'Invalid Nucleotide')
end)
it('counts all nucleotides', function()
local dna = DNA:new('AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC')
expected = { A = 20, T = 21, G = 17, C = 12 }
result = dna.nucleotideCounts
assert.are.same(expected, result)
end)
end)
local n_count = {}
function n_count:count(base)
assert(self.nucleotideCounts[base], 'Invalid Nucleotide')
return self.nucleotideCounts[base]
end
function n_count:new(dna)
local o = {}
setmetatable(o, self)
self.__index = self
o.nucleotideCounts = { A = 0, T = 0, C = 0, G = 0 }
for i = 1, #dna do
local c = dna:sub(i,i)
assert(o.nucleotideCounts[c], 'Invalid Nucleotide')
o.nucleotideCounts[c] = o.nucleotideCounts[c] + 1
end
return o
end
return n_count
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments