Sha256: 8b7366189fe1f1ab1f2dcfa8621abe5c88c142530cdcbd5edaec841967ea84af

Contents?: true

Size: 612 Bytes

Versions: 327

Compression:

Stored size: 612 Bytes

Contents

defmodule Hamming do
  @doc """
  Returns number of differences between two strands of Hamming, known as the Hamming Distance.

  ## Examples

  iex> Hamming.hamming_distance('AAGTCATA', 'TAGCGATC')
  {:ok, 4}
  """
  def hamming_distance(strand1, strand2) when length(strand1) === length(strand2) do
    distance = pairs(strand1, strand2) |> count_mismatched
    {:ok, distance}
  end
  def hamming_distance(_, _), do: {:error, "Lists must be the same length"}

  defp pairs(s1, s2),           do: Enum.zip(s1, s2)
  defp count_mismatched(pairs), do: Enum.count(pairs, fn({c1, c2}) -> c2 && (c1 != c2) end)
end

Version data entries

327 entries across 327 versions & 1 rubygems

Version Path
trackler-1.0.2.1 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.2.0 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.1.2 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.1.1 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.1.0 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.0.1 tracks/elixir/exercises/hamming/example.exs
trackler-1.0.0 tracks/elixir/exercises/hamming/example.exs