| 10 | const std::map<char, int> BASE_VAL = {{'A',0},{'T',1},{'C',2},{'G',3}}; |
| 11 | |
| 12 | std::string encode(const std::string& text) { |
| 13 | std::string result; |
| 14 | result.reserve(text.size() * 4); |
| 15 | for (unsigned char c : text) { |
| 16 | result += BASES[(c >> 6) & 3]; |
| 17 | result += BASES[(c >> 4) & 3]; |
| 18 | result += BASES[(c >> 2) & 3]; |
| 19 | result += BASES[c & 3]; |
| 20 | } |
| 21 | return result; |
| 22 | } |
| 23 | |
| 24 | std::string decode(const std::string& dna) { |
| 25 | if (dna.size() % 4 != 0) throw std::runtime_error("Invalid DNA length"); |