Sha256: a575bed501830ed5e80632d6c0209eee74b235152ce1542b8b6698538bdc8278

Contents?: true

Size: 793 Bytes

Versions: 69

Compression:

Stored size: 793 Bytes

Contents

defmodule Diamond do
  @doc """
  Given a letter, it prints a diamond starting with 'A',
  with the supplied letter at the widest point.
  """
  @spec build_shape(char) :: String.t()
  def build_shape(letter) do
    pos = letter - 64
    size = pos * 2 - 1
    half = build_half_diamond(pos, size)
    Enum.join([half | Enum.slice(Enum.reverse(half), 1, length(half))])
  end

  defp build_half_diamond(rows, row_size) do
    Enum.reduce(0..(rows - 1), [], fn x, acc ->
      List.duplicate("\s", row_size)
      |> List.update_at(rows - 1 - x, fn _ -> <<65 + x>> end)
      |> Enum.reverse()
      |> List.update_at(rows - 1 - x, fn _ -> <<65 + x>> end)
      |> List.insert_at(row_size + 1, "\n")
      |> List.duplicate(1)
      |> Enum.concat(acc)
    end)
    |> Enum.reverse()
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

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