Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Last active March 31, 2016 20:54
Show Gist options
  • Save colinrymer/7573eed08c25760bd611 to your computer and use it in GitHub Desktop.
Save colinrymer/7573eed08c25760bd611 to your computer and use it in GitHub Desktop.
defmodule DNA do
@doc ~S"""
Count the respective number of times that the symbols 'A', 'C', 'G', and 'T' in a given string.
Returns the count as four integers separated by spaces, representing occureneces of "A", "C", "G", and "T" respectively.
## Examples
iex> DNA.count("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC")
"20 12 17 21"
"""
@spec count(string :: String.t) :: String.t
def count(string), do: _count(string, [0,0,0,0])
def _count("", [a,c,g,t]), do: "#{a} #{c} #{g} #{t}"
def _count("A" <> rest, [a,c,g,t]), do: _count(rest, [a+1,c,g,t])
def _count("C" <> rest, [a,c,g,t]), do: _count(rest, [a,c+1,g,t])
def _count("G" <> rest, [a,c,g,t]), do: _count(rest, [a,c,g+1,t])
def _count("T" <> rest, [a,c,g,t]), do: _count(rest, [a,c,g,t+1])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment