 # rootulp's solution

## to Nucleotide Count in the Elm Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

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.

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 Exercism help page for Elm installation and learning resources.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed.

``````\$ elm-package install --yes
``````

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's possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (..)

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

tests : Test
tests =
describe "NucleotideCount"
[ test "the solution is for the correct version of the test" <|
\() -> Expect.equal 2 version
, 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")
]``````

### elm-package.json

``````{
"version": "3.0.0",
"summary": "Exercism problems in Elm.",
"repository": "https://github.com/exercism/elm.git",
"source-directories": [
".",
".."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}``````
``````module NucleotideCount exposing (..)

import Dict exposing (Dict)

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

nucleotideCounts: String -> NucleotideCounts
nucleotideCounts strand =
let occurencesInStrand = occurencesIn strand in
{ a = occurencesInStrand 'A'
, t = occurencesInStrand 'T'
, c = occurencesInStrand 'C'
, g = occurencesInStrand 'G'
}

occurencesIn: String -> Char -> Int
occurencesIn strand nucleotide =
let nucleotideOccurences = occurences strand in
nucleotideOccurences
|> Dict.get nucleotide
|> Maybe.withDefault 0

occurences: String -> Dict Char Int
occurences strand =
strand
|> String.toList
|> List.foldl populateDict Dict.empty

populateDict : Char -> Dict Char Int -> Dict Char Int
populateDict nucleotide dict =
Dict.update nucleotide incrementValue dict

incrementValue : Maybe Int -> Maybe Int
incrementValue maybe =
Maybe.withDefault 0 maybe
+ 1
|> Just

version : Int
version =
2``````