mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-17 10:11:49 -06:00
nucleotide count + robot simulator solutions elixir
This commit is contained in:
32
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal file
32
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal 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
|
||||
Reference in New Issue
Block a user