exercism/elixir/hamming/lib/hamming.ex

16 lines
569 B
Elixir
Raw Normal View History

2024-06-27 03:52:34 +00:00
defmodule Hamming do
@doc """
Returns number of differences between two strands of DNA, known as the Hamming Distance.
## Examples
iex> Hamming.hamming_distance(~c"AAGTCATA", ~c"TAGCGATC")
{:ok, 4}
"""
@spec hamming_distance([char], [char]) :: {:ok, non_neg_integer} | {:error, String.t()}
def hamming_distance(strand1, strand2) when length(strand1) != length(strand2), do: {:error, "strands must be of equal length"}
def hamming_distance(strand1, strand2) do
{:ok, Enum.count(Enum.zip(strand1, strand2), fn {s1, s2} -> s1 != s2 end)}
end
end