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-2.2.1.109 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.90 tracks/elixir/exercises/hamming/example.exs