Avatar of shmibs

shmibs's solution

to RNA Transcription in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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)
{
}

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

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

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

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

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

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

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

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

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

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);

   UnityEnd();
   return 0;
}
#include <stdlib.h>
#include <string.h>

#include <limits.h>
#include <assert.h>

#include "rna_transcription.h"

#ifndef RNA_START_SIZE
#define RNA_START_SIZE 1024
#endif

#ifndef RNA_INCREMENT_SIZE
#define RNA_INCREMENT_SIZE 1024
#endif


static inline char dc2rc(char c)
{
	switch(c) {
	case 'G': return 'C';
	case 'C': return 'G';
	case 'T': return 'A';
	case 'A': return 'U';
	default: return '\0';
	}
}

static void make_space(char **sr, unsigned *i, unsigned *mlen)
{
	/* space left? */
	if (*i != *mlen)
		return;

	/* bad macro vals? */
	if (RNA_START_SIZE <= 0 || RNA_INCREMENT_SIZE <= 0) {
		free(*sr);
		*sr = NULL;
		return;
	}

	/* overflow of unsigned? */
	while (*mlen < (*sr == NULL ? RNA_START_SIZE : RNA_INCREMENT_SIZE)) {
		(*mlen)++;
		if (*mlen == 0) {
			free(*sr);
			*sr = NULL;
			return;
		}
	}

	/* first time? */
	if (*sr == NULL)
		*mlen = RNA_START_SIZE;
	else
		*mlen += RNA_INCREMENT_SIZE;

	*sr = realloc(*sr, *mlen * sizeof(char));
}

char* to_rna(const char *sd)
{
	char *sr = NULL;
	unsigned i, mlen;

	if (sd == NULL)
		return NULL;

	for(i = 0, mlen = 0; sd[i] != '\0';) {
		if (i == UINT_MAX)
			goto err;

		make_space(&sr, &i, &mlen);

		if (sr == NULL)
			return NULL;

		sr[i] = dc2rc(sd[i]);

		if (sr[i] == '\0')
			goto err;

		i++;
	}

	make_space(&sr, &i, &mlen);

	if (sr == NULL)
		return NULL;

	sr[i] = '\0';

	return sr;

err:
	free(sr);
	return NULL;
}

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?