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.119 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.118 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.117 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.116 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.115 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.114 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.113 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.111 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.110 tracks/elixir/exercises/diamond/example.exs