Sha256: 2c5d3febbba296ad4b76bd113ad11b97f638f2994993494d5a1ce88190f2ecf4

Contents?: true

Size: 676 Bytes

Versions: 69

Compression:

Stored size: 676 Bytes

Contents

defmodule CryptoSquare do
  @doc """
  Encode string square methods
  ## Examples

    iex> CryptoSquare.encode("abcd")
    "ac bd"
  """
  @spec encode(String.t()) :: String.t()
  def encode(""), do: ""

  def encode(str) do
    normalized = normalize_string(str)
    section_length = normalized |> byte_size |> :math.sqrt() |> Float.ceil() |> trunc

    normalized
    |> String.graphemes()
    |> Enum.chunk(section_length, section_length, List.duplicate("", section_length))
    |> List.zip()
    |> Enum.map(&Tuple.to_list/1)
    |> Enum.join(" ")
  end

  defp normalize_string(str) do
    str
    |> String.downcase()
    |> String.replace(~r/[^a-z0-9]/, "")
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/crypto-square/example.exs