Sha256: 79088a2c3d4395cb6d3af89ebadae3e1a21c64155a083616892ddfb5cb7aae81

Contents?: true

Size: 799 Bytes

Versions: 327

Compression:

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

327 entries across 327 versions & 1 rubygems

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