nucleotide count + robot simulator solutions elixir

This commit is contained in:
Xevion
2021-01-18 22:45:34 -06:00
parent 2e71b743ac
commit e469b45491
16 changed files with 479 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
defmodule NucleotideCount do
@doc """
Counts individual nucleotides in a DNA strand.
## Examples
iex> NucleotideCount.count('AATAA', ?A)
4
iex> NucleotideCount.count('AATAA', ?T)
1
"""
@spec count(charlist(), char()) :: non_neg_integer()
def count(strand, nucleotide) do
Enum.count(strand, &(&1 == nucleotide))
end
@doc """
Returns a summary of counts by nucleotide.
## Examples
iex> NucleotideCount.histogram('AATAA')
%{?A => 4, ?T => 1, ?C => 0, ?G => 0}
"""
@spec histogram(charlist()) :: map()
def histogram(strand) do
Enum.reduce(strand, %{?A => 0, ?T => 0, ?C => 0, ?G => 0}, fn nucleotide, counts ->
Map.update(counts, nucleotide, 1, &(&1 + 1))
end)
end
end