diff --git a/cpp/nucleotide-count/nucleotide_count.cpp b/cpp/nucleotide-count/nucleotide_count.cpp new file mode 100644 index 0000000..fea82c4 --- /dev/null +++ b/cpp/nucleotide-count/nucleotide_count.cpp @@ -0,0 +1,46 @@ +#include "nucleotide_count.h" + +#include +#include + + +namespace dna { + +using namespace std; + + +static const unordered_set 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); +} + +} diff --git a/cpp/nucleotide-count/nucleotide_count.h b/cpp/nucleotide-count/nucleotide_count.h new file mode 100644 index 0000000..9017300 --- /dev/null +++ b/cpp/nucleotide-count/nucleotide_count.h @@ -0,0 +1,24 @@ +#pragma once + +#define EXERCISM_RUN_ALL_TESTS + +#include +#include + + +namespace dna { + +typedef std::map NucleotideCounts; + +class counter +{ + NucleotideCounts nucleotide_counts_; + +public: + + counter(const std::string &str); + NucleotideCounts nucleotide_counts() const; + int count(char c) const; +}; + +}