Sha256: 8a772fd6bd05a34f38b885b92c6c1978c12817e811ff34cb3aeb3b3175b70b02

Contents?: true

Size: 710 Bytes

Versions: 179

Compression:

Stored size: 710 Bytes

Contents

defmodule Atbash do
  @key Enum.zip(?a..?z, ?z..?a) |> Enum.into(%{})

  @doc """
  Encode a given plaintext to the corresponding ciphertext

  ## Examples

  iex> Atbash.encode("completely insecure")
  "xlnko vgvob rmhvx fiv"
  """
  def encode(plaintext) do
    plaintext |> normalize |> cipher |> chunk |> Enum.join(" ")
  end

  defp normalize(input) do
    Regex.replace(~r{\W}, String.downcase(input), "")
  end

  defp cipher(plaintext) do
    plaintext
      |> String.to_char_list
      |> Enum.map(&convert/1)
      |> List.to_string
  end

  defp convert(character) do
    Map.get(@key, character, character)
  end

  defp chunk(input) do
    Regex.scan(~r(.{1,5}), input) |> List.flatten
  end
end

Version data entries

179 entries across 179 versions & 1 rubygems

Version Path
trackler-2.1.0.23 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.22 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.21 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.20 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.19 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.18 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.17 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.16 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.15 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.14 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.13 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.12 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.11 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.10 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.9 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.8 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.7 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.6 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.5 tracks/elixir/exercises/atbash-cipher/example.exs
trackler-2.1.0.4 tracks/elixir/exercises/atbash-cipher/example.exs