Avatar of fawick

fawick's solution

to RNA Transcription in the C++ Track

Published at Oct 11 2019 · 0 comments
Instructions
Test suite
Solution

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.

#if defined(EXERCISM_RUN_ALL_TESTS)

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.

Source

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.

rna_transcription_test.cpp

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

TEST_CASE("transcribes_cytidine_to_guanosine")
{
    REQUIRE('G' == rna_transcription::to_rna('C'));
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("transcribes_guanosine_to_cytidine")
{
    REQUIRE('C' == rna_transcription::to_rna('G'));
}

TEST_CASE("transcribes_adenosine_to_uracil")
{
    REQUIRE('U' == rna_transcription::to_rna('A'));
}

TEST_CASE("transcribes_thymidine_to_adenosine")
{
    REQUIRE('A' == rna_transcription::to_rna('T'));
}

TEST_CASE("transcribes_all_dna_nucleotides_to_their_rna_complements")
{
    REQUIRE("UGCACCAGAAUU" == rna_transcription::to_rna("ACGTGGTCTTAA"));
}
#endif

rna_transcription.cpp

#include "rna_transcription.h"

namespace rna_transcription {

char to_rna(char c) {
  switch (c) {
  case 'C':
    return 'G';
  case 'G':
    return 'C';
  case 'A':
    return 'U';
  case 'T':
    return 'A';
  }
  return c;
}

std::string to_rna(std::string dna) {
  std::string result;
  for (char c : dna) {
    result += to_rna(c);
  }
  return result;
}

} // namespace rna_transcription

rna_transcription.h

#if !defined(RNA_TRANSCRIPTION_H)
#define RNA_TRANSCRIPTION_H

#include <string>

namespace rna_transcription {
char to_rna(char c);
std::string to_rna(std::string);

} // namespace rna_transcription

#endif // RNA_TRANSCRIPTION_H

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?