🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of pontus-stromberg

pontus-stromberg's solution

to RNA Transcription in the C Track

Published at Jun 01 2020 · 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 "Guides" section of the C track on the Exercism site. 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.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need 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, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

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.

test_rna_transcription.c

#include "vendor/unity.h"
#include "../src/rna_transcription.h"
#include <stdlib.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_transcription(const char *dna, const char *expected)
{
   char *rna = to_rna(dna);
   TEST_ASSERT_EQUAL_STRING(expected, rna);
   free(rna);
}

static void test_failure(const char *dna)
{
   TEST_ASSERT_NULL(to_rna(dna));
}

static void test_transcribes_G_to_C(void)
{
   test_transcription("G", "C");
}

static void test_transcribes_C_to_G(void)
{
   TEST_IGNORE();               // delete this line to run test
   test_transcription("C", "G");
}

static void test_transcribes_T_to_A(void)
{
   TEST_IGNORE();
   test_transcription("T", "A");
}

static void test_transcribes_A_to_U(void)
{
   TEST_IGNORE();
   test_transcription("A", "U");
}

static void test_transcribes_all_occurrences(void)
{
   TEST_IGNORE();
   test_transcription("ACGTGGTCTTAA", "UGCACCAGAAUU");
}

static void test_handle_invalid_nucleotide(void)
{
   TEST_IGNORE();
   test_failure("U");
}

static void test_handle_completely_invalid_input(void)
{
   TEST_IGNORE();
   test_failure("XXX");
}

static void test_handle_partially_invalid_input(void)
{
   TEST_IGNORE();
   test_failure("ACGTXXXCTTAA");
}

int main(void)
{
   UnityBegin("test/test_rna_transcription.c");

   RUN_TEST(test_transcribes_G_to_C);
   RUN_TEST(test_transcribes_C_to_G);
   RUN_TEST(test_transcribes_T_to_A);
   RUN_TEST(test_transcribes_A_to_U);
   RUN_TEST(test_transcribes_all_occurrences);
   RUN_TEST(test_handle_invalid_nucleotide);
   RUN_TEST(test_handle_completely_invalid_input);
   RUN_TEST(test_handle_partially_invalid_input);

   return UnityEnd();
}

src/rna_transcription.c

#include "rna_transcription.h"
#include <string.h>
#include <stdlib.h>

char * to_rna(const char * dna){

   int len = strlen(dna);
   char * rna = (char *)malloc(len);
   for(int i = 0; i<len; i++){
      switch(dna[i]){
         case 'G':
            rna[i] = 'C';
            break;
         case 'C':
            rna[i] = 'G';
            break;
         case 'T':
            rna[i] = 'A';
            break;
         case 'A':
            rna[i] = 'U';
            break;
         default:
            return NULL;
      }
   }
   return rna;

}

src/rna_transcription.h

#ifndef RNA_TRANSCRIPTION_H
#define RNA_TRANSCRIPTION_H


char * to_rna(const char * dna);


#endif

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?