Skip to content

Nucleotide Count

Intro

The genetic language of every living thing on the planet is DNA.

DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides.

4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

  • twigs are to birds nests as
  • nucleotides are to DNA as
  • legos are to lego houses as
  • words are to sentences as...

Task

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "nucleotide_count.h"
#include <cctype>
#include <cstddef>
#include <stdexcept>
#include <string>

namespace nucleotide_count {
} // namespace nucleotide_count

nucleotide_count::counter::counter(const std::string &str)
    : tmp{{'A', 0}, {'T', 0}, {'C', 0}, {'G', 0}} {
for (auto &c : str) {
    if (tmp.find(c) == tmp.end())
    throw std::invalid_argument("salah argument:");
    tmp.at(c) += 1;
}
}

std::map<char, int> const nucleotide_count::counter::nucleotide_counts() const { return tmp; }

int nucleotide_count::counter::count(const char &c) const {
if (tmp.find(c) == tmp.end()) {
    throw std::invalid_argument("salah argument");
}
return tmp.at(c);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <cstdint>
#if !defined(NUCLEOTIDE_COUNT_H)
#define NUCLEOTIDE_COUNT_H
#include "map"
#include "string"

namespace nucleotide_count {
class counter {
public:
std::map<char, int> tmp{};
counter(const std::string&);
const std::map<char, int> nucleotide_counts() const;
int count(const char &c) const;
};
} // namespace nucleotide_count
#endif // NUCLEOTIDE_COUNT_H

Last update: February 10, 2021

Comments