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.180 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/hamming/example.exs