Avatar of paulfioravanti

paulfioravanti's solution

to Nucleotide Count in the Elm Track

Published at Jun 16 2019 · 0 comments
Instructions
Test suite
Solution

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

  • twigs are to birds nests as
  • nucleotides are to DNA as
  • legos are to lego houses as
  • words are to sentences as...

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Source

The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import NucleotideCount exposing (nucleotideCounts)
import Test exposing (..)


tests : Test
tests =
    describe "NucleotideCount"
        [ test "empty dna strand has no nucleotides" <|
            \() ->
                Expect.equal { a = 0, t = 0, c = 0, g = 0 }
                    (nucleotideCounts "")
        , skip <|
            test "repetitive sequence has only guanine" <|
                \() ->
                    Expect.equal { a = 0, t = 0, c = 0, g = 8 }
                        (nucleotideCounts "GGGGGGGG")
        , skip <|
            test "counts all nucleotides" <|
                \() ->
                    Expect.equal { a = 20, t = 21, c = 12, g = 17 }
                        (nucleotideCounts "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
        ]
module NucleotideCount exposing (nucleotideCounts)


type alias NucleotideCounts =
    { a : Int
    , t : Int
    , c : Int
    , g : Int
    }


nucleotideCounts : String -> NucleotideCounts
nucleotideCounts sequence =
    let
        initialCounts =
            { a = 0, t = 0, c = 0, g = 0 }
    in
    sequence
        |> String.foldl countNucleotide initialCounts



-- PRIVATE


countNucleotide : Char -> NucleotideCounts -> NucleotideCounts
countNucleotide nucleotide counts =
    case nucleotide of
        'A' ->
            { counts | a = counts.a + 1 }

        'T' ->
            { counts | t = counts.t + 1 }

        'C' ->
            { counts | c = counts.c + 1 }

        'G' ->
            { counts | g = counts.g + 1 }

        _ ->
            counts

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?