Avatar of 4d47

4d47's solution

to RNA Transcription in the PHP Track

Published at Jul 13 2018 · 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

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
  3. Execute the tests:

     % ./phpunit.phar rna-transcription/rna-transcription_test.php


Rosalind http://rosalind.info/problems/rna

Submitting Incomplete Solutions

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



require "rna-transcription.php";

class ComplementTest extends PHPUnit\Framework\TestCase
    public function testTranscribesGuanineToCytosine()
        $this->assertSame('G', toRna('C'));

    public function testTranscribesCytosineToGuanine()
        $this->assertSame('C', toRna('G'));

    public function testTranscribesThymineToAdenine()
        $this->assertSame('A', toRna('T'));

    public function testTranscribesAdenineToUracil()
        $this->assertSame('U', toRna('A'));

    public function testTranscribesAllOccurencesOne()
        $this->assertSame('UGCACCAGAAUU', toRna('ACGTGGTCTTAA'));

function toRna($dna)
    static $nucleotide_complements = [
        'G' => 'C',
        'C' => 'G',
        'T' => 'A',
        'A' => 'U',
    if (!preg_match('/[GCTA]+/', $dna)) {
        throw new InvalidArgumentException('Invalid DNA strand, only G, C, T, A characters allowed.');
    return strtr($dna, $nucleotide_complements);

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?