Avatar of xxylem

xxylem's solution

to RNA Transcription in the C++ Track

Published at Oct 19 2019 · 0 comments
Test suite

Given a DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.


This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.


Hyperphysics http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#include "rna_transcription.h"
#include "test/catch.hpp"

    REQUIRE('G' == rna_transcription::to_rna('C'));

    REQUIRE('C' == rna_transcription::to_rna('G'));

    REQUIRE('U' == rna_transcription::to_rna('A'));

    REQUIRE('A' == rna_transcription::to_rna('T'));

    REQUIRE("UGCACCAGAAUU" == rna_transcription::to_rna("ACGTGGTCTTAA"));


#include "rna_transcription.h"

namespace rna_transcription {

	using namespace std;

	const char to_rna(const char& nucleotide)
		return nucleotide_translations.at(nucleotide);

	const string to_rna(const string& dna_strand)
		string rna_strand;

		for (const char& nucleotide : dna_strand)
			rna_strand += to_rna(nucleotide);

		return rna_strand;
}  // namespace rna_transcription



#include <map>
#include <string>

namespace rna_transcription {

	const std::map<char, char> nucleotide_translations = 
		{ 'G', 'C'}
		, {'C', 'G'}
		, {'T', 'A'}
		, {'A', 'U'}

	const char to_rna(const char& nucleotide);
	const std::string to_rna(const std::string& dna_strand);

}  // namespace rna_transcription


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?