nucleotide_count: initial commit
This commit is contained in:
parent
97e0caa731
commit
94adf21f78
2 changed files with 70 additions and 0 deletions
46
cpp/nucleotide-count/nucleotide_count.cpp
Normal file
46
cpp/nucleotide-count/nucleotide_count.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#include "nucleotide_count.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace dna {
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
static const unordered_set<char> valid_nucleotides = {'A', 'T', 'C', 'G'};
|
||||
|
||||
static inline bool is_valid_nucleotide(char c)
|
||||
{
|
||||
return valid_nucleotides.find(c) != valid_nucleotides.cend();
|
||||
}
|
||||
|
||||
counter::counter(const string &str)
|
||||
{
|
||||
for (auto c : valid_nucleotides)
|
||||
nucleotide_counts_[c] = 0;
|
||||
|
||||
for (auto c : str) {
|
||||
|
||||
if (!is_valid_nucleotide(c))
|
||||
throw invalid_argument("invalid sequence");
|
||||
|
||||
++nucleotide_counts_[c];
|
||||
}
|
||||
}
|
||||
|
||||
NucleotideCounts counter::nucleotide_counts() const
|
||||
{
|
||||
return nucleotide_counts_;
|
||||
}
|
||||
|
||||
int counter::count(char c) const
|
||||
{
|
||||
if (!is_valid_nucleotide(c))
|
||||
throw invalid_argument("invalid nucleotide");
|
||||
|
||||
return nucleotide_counts_.at(c);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue