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.69 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.68 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.67 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.66 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.65 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.64 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.63 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.62 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.61 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.60 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.59 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.58 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.57 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.56 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.55 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.54 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.53 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.52 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.51 tracks/elixir/exercises/hamming/example.exs
trackler-2.2.1.50 tracks/elixir/exercises/hamming/example.exs