ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# tex's solution

## to RNA Transcription in the PureScript Track

Published at May 07 2020 · 0 comments
Instructions
Test suite
Solution

# 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`

## Submitting Incomplete Solutions

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

### Main.purs

``````module Test.Main where

import Prelude

import Effect (Effect)
import Data.Maybe (Maybe(..))
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
import Test.Unit.Main (runTest)
import RNATranscription (toRNA)

main :: Effect Unit
main = runTest suites

suites :: TestSuite
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 Prelude

import Data.Array (many)
import Data.Either (hush)
import Data.Maybe (Maybe)
import Data.String.CodeUnits (fromCharArray)
import Text.Parsing.Parser (runParser)
import Text.Parsing.Parser.Combinators (choice)
import Text.Parsing.Parser.String (char, eof)

toRNA âˆ· String â†’ Maybe String
toRNA s = fromCharArray <\$> (hush \$ runParser s \$ go <* eof)
where
go = many
\$ choice
[ char 'C' \$> 'G'
, char 'G' \$> 'C'
, char 'T' \$> 'A'
, char 'A' \$> 'U'
]``````