Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nucleotide-count: convert the class to a function #579

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions exercises/practice/nucleotide-count/.meta/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
namespace nucleotide_count
{

counter::counter(std::string const& sequence)
: counts_({ {'A', 0}, {'C', 0}, {'G', 0}, {'T', 0} })
std::map<char, int> count(std::string_view dna)
{
for (auto nucleotide : sequence) {
auto it = counts_.find(nucleotide);
if (it == counts_.end()) {
std::map<char, int> 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;
}

}
18 changes: 2 additions & 16 deletions exercises/practice/nucleotide-count/.meta/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@
#define NUCLEOTIDE_COUNT_H

#include <map>
#include <string>
#include <string_view>

namespace nucleotide_count
{

class counter
{
public:
counter(std::string const& sequence);

std::map<char, int> const& nucleotide_counts() const
{
return counts_;
}

int count(char nucleotide) const;

private:
std::map<char, int> counts_;
};
std::map<char, int> count(std::string_view dna);

}

Expand Down
18 changes: 5 additions & 13 deletions exercises/practice/nucleotide-count/nucleotide_count_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

TEST_CASE("empty_strand")
{
const nucleotide_count::counter dna("");
const std::map<char, int> expected{ {'A', 0}, {'C', 0}, {'G', 0}, {'T', 0} };

const auto actual = dna.nucleotide_counts();
const auto actual = nucleotide_count::count("");

REQUIRE(expected == actual);
}
Expand All @@ -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<char, int> 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<char, int> 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<char, int> 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