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.89 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.88 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.87 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.86 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.85 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.84 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.83 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.82 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.81 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.80 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.79 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.78 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.77 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.76 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.75 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.74 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.73 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.72 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.71 tracks/elixir/exercises/diamond/example.exs
trackler-2.2.1.70 tracks/elixir/exercises/diamond/example.exs