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

yehzhang's solution

to RNA Transcription in the PureScript Track

Published at Jul 13 2018 · 0 comments
Test suite


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.

Rna Transcription

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


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.


module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
import Data.Maybe (Maybe(..))
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)
import RNATranscription (toRNA)

main :: forall eff
  . Eff ( avar :: AVAR
        , console :: CONSOLE
        , testOutput :: TESTOUTPUT
        | eff
main = runTest suites

suites :: forall e. TestSuite e
suites =
  suite "RNATranscription.toRNA" do
    test "RNA complement of cytosine is guanine" $
      Assert.equal (Just "G") (toRNA "C")

    test "RNA complement of guanine is cytosine" $
      Assert.equal (Just "C") (toRNA "G")

    test "RNA complement of thymine is adenine" $
      Assert.equal (Just "A") (toRNA "T")

    test "RNA complement of adenine is uracil" $
      Assert.equal (Just "U") (toRNA "A")

    test "RNA complement" $
      Assert.equal (Just "UGCACCAGAAUU") (toRNA "ACGTGGTCTTAA")

    test "correctly handles invalid input (RNA instead of DNA)" $
      Assert.equal Nothing (toRNA "U")

    test "correctly handles completely invalid DNA input" $
      Assert.equal Nothing (toRNA "XXX")

    test "correctly handles partially invalid DNA input" $
      Assert.equal Nothing (toRNA "ACGTXXXCTTAA")
module RNATranscription
  ( toRNA
  ) where

import Data.Traversable (traverse)
import Data.Maybe (Maybe(..))
import Data.String (Pattern(..), split, joinWith)
import Prelude ((#), (<#>))

toRNA :: String -> Maybe String
toRNA dna = split (Pattern "") dna # traverse toRNA1 <#> joinWith ""

toRNA1 :: String -> Maybe String
toRNA1 "G" = Just "C"
toRNA1 "C" = Just "G"
toRNA1 "T" = Just "A"
toRNA1 "A" = Just "U"
toRNA1 _   = Nothing

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?