diff --git a/exercises/practice/nucleotide-count/.meta/example.cpp b/exercises/practice/nucleotide-count/.meta/example.cpp index cc4a87100..acc69bda7 100644 --- a/exercises/practice/nucleotide-count/.meta/example.cpp +++ b/exercises/practice/nucleotide-count/.meta/example.cpp @@ -4,16 +4,17 @@ namespace nucleotide_count { -counter::counter(std::string const& sequence) - : counts_({ {'A', 0}, {'C', 0}, {'G', 0}, {'T', 0} }) +std::map count(std::string_view dna) { - for (auto nucleotide : sequence) { - auto it = counts_.find(nucleotide); - if (it == counts_.end()) { + std::map counter{{'A', 0}, {'C', 0}, {'G', 0}, {'T', 0}}; + for (auto nucleotide : dna) { + auto it = counter.find(nucleotide); + if (it == counter.end()) { throw std::invalid_argument("Unknown nucleotide"); } ++(it->second); } + return counter; } } diff --git a/exercises/practice/nucleotide-count/.meta/example.h b/exercises/practice/nucleotide-count/.meta/example.h index f31fd0ae6..ac5cfd999 100644 --- a/exercises/practice/nucleotide-count/.meta/example.h +++ b/exercises/practice/nucleotide-count/.meta/example.h @@ -2,26 +2,12 @@ #define NUCLEOTIDE_COUNT_H #include -#include +#include namespace nucleotide_count { -class counter -{ -public: - counter(std::string const& sequence); - - std::map const& nucleotide_counts() const - { - return counts_; - } - - int count(char nucleotide) const; - -private: - std::map counts_; -}; +std::map count(std::string_view dna); } diff --git a/exercises/practice/nucleotide-count/nucleotide_count_test.cpp b/exercises/practice/nucleotide-count/nucleotide_count_test.cpp index 0fabf316b..fc071258e 100644 --- a/exercises/practice/nucleotide-count/nucleotide_count_test.cpp +++ b/exercises/practice/nucleotide-count/nucleotide_count_test.cpp @@ -9,10 +9,8 @@ TEST_CASE("empty_strand") { - const nucleotide_count::counter dna(""); const std::map expected{ {'A', 0}, {'C', 0}, {'G', 0}, {'T', 0} }; - - const auto actual = dna.nucleotide_counts(); + const auto actual = nucleotide_count::count(""); REQUIRE(expected == actual); } @@ -21,36 +19,30 @@ TEST_CASE("empty_strand") TEST_CASE("can_count_one_nucleotide_in_single_character_input") { - const nucleotide_count::counter dna("G"); const std::map expected{ {'A', 0}, {'C', 0}, {'G', 1}, {'T', 0} }; - - const auto actual = dna.nucleotide_counts(); + const auto actual = nucleotide_count::count("G"); REQUIRE(expected == actual); } TEST_CASE("strand_with_repeated_nucleotide") { - const nucleotide_count::counter dna("GGGGGGG"); const std::map expected{ {'A', 0}, {'C', 0}, {'G', 7}, {'T', 0} }; - - const auto actual = dna.nucleotide_counts(); + const auto actual = nucleotide_count::count("GGGGGGG"); REQUIRE(expected == actual); } TEST_CASE("strand_with_multiple_nucleotides") { - const nucleotide_count::counter dna("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"); const std::map expected{ {'A', 20}, {'C', 12}, {'G', 17}, {'T', 21} }; - - const auto actual = dna.nucleotide_counts(); + const auto actual = nucleotide_count::count("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"); REQUIRE(expected == actual); } TEST_CASE("strand_with_invalid_nucleotides") { - REQUIRE_THROWS_AS(nucleotide_count::counter("AGXXACT"), std::invalid_argument); + REQUIRE_THROWS_AS(nucleotide_count::count("AGXXACT"), std::invalid_argument); } #endif