Sha256: 48ac72831c6130cecf7d87768e0f18bd11d0d94fd58e5885649715db0a9387cb

Contents?: true

Size: 600 Bytes

Versions: 69

Compression:

Stored size: 600 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

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.118 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.117 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.116 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.115 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.114 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.113 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.111 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.110 tracks/elixir/exercises/hamming/example.exs