Sha256: bedb1179eeb1008672dbd02d7ab4de1de93ebb9c1c67d6a9b8f5b6dd40dc65de

Contents?: true

Size: 661 Bytes

Versions: 327

Compression:

Stored size: 661 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

327 entries across 327 versions & 1 rubygems

Version Path
trackler-2.2.1.109 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/crypto-square/example.exs
trackler-2.2.1.90 tracks/elixir/exercises/crypto-square/example.exs