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);
|
||||
}
|
||||
|
||||
}
|
||||
24
cpp/nucleotide-count/nucleotide_count.h
Normal file
24
cpp/nucleotide-count/nucleotide_count.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#define EXERCISM_RUN_ALL_TESTS
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace dna {
|
||||
|
||||
typedef std::map<char, int> NucleotideCounts;
|
||||
|
||||
class counter
|
||||
{
|
||||
NucleotideCounts nucleotide_counts_;
|
||||
|
||||
public:
|
||||
|
||||
counter(const std::string &str);
|
||||
NucleotideCounts nucleotide_counts() const;
|
||||
int count(char c) const;
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue