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:
Refer to the Installing Elm page for information about installing elm.
The code you have to write is located inside the
src/ directory of the exercise.
Elm automatically installs packages dependencies the first time you run the tests
so we can start by running the tests from the exercise directory with:
To automatically run tests again when you save changes:
$ elm-test --watch
As you work your way through the tests suite in the file
be sure to remove the
calls from each test until you get them all passing!
It is possible to submit an incomplete solution so you can see how others have completed the exercise.
module Tests exposing (tests) import Expect import RNATranscription exposing (toRNA) import Test exposing (..) isErr : Result error value -> Bool isErr result = case result of Ok _ -> False Err _ -> True tests : Test tests = describe "RNATranscription" [ test "complement of cytosine is guanine" <| \() -> Expect.equal (Ok "G") (toRNA "C") , skip <| test "complement of guanine is cytosine" <| \() -> Expect.equal (Ok "C") (toRNA "G") , skip <| test "complement of thymine is adenine" <| \() -> Expect.equal (Ok "A") (toRNA "T") , skip <| test "complement of adenine is uracil" <| \() -> Expect.equal (Ok "U") (toRNA "A") , skip <| test "complement" <| \() -> Expect.equal (Ok "UGCACCAGAAUU") (toRNA "ACGTGGTCTTAA") , skip <| test "input \"INVALID\" should produce an error" <| \() -> Expect.true "expected an error message output. For example `Err \"Invalid input\"`" (toRNA "INVALID" |> isErr) ]
module RNATranscription exposing (toRNA) toRNA : String -> Result String String toRNA dna = let translated = dna |> String.toList |> List.map translateChar in if List.any isError translated then Err "Invalid input." else Ok (String.fromList <| List.map unwrap translated) unwrap : Result error Char -> Char unwrap res = case res of Ok c -> c Err _ -> ' ' isError : Result error value -> Bool isError res = case res of Ok _ -> False Err _ -> True translateChar : Char -> Result String Char translateChar c = case c of 'C' -> Ok 'G' 'G' -> Ok 'C' 'T' -> Ok 'A' 'A' -> Ok 'U' _ -> Err "cannot translate"
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.