mouredev/Weekly-Challenge-2022-Kotlin

Solucion Reto #26 Elixir

fabigunner79 opened this issue · 0 comments

#Fabian Garrido
#Elixir

 defmodule WeeklyChallenge2022 do
  @spec main() :: :ok
  def main do
    draw_polygon(10, :square)
    draw_polygon(15, :triangle)
    draw_polygon(12, :diamond)
  end

  @spec draw_polygon(integer(), atom()) :: :ok
  defp draw_polygon(size, type) when is_integer(size) and size >= 2 do
    total_size = if type == :diamond, do: size * 2, else: size

    1..total_size
    |> Enum.each(fn value ->
      case {type, value} do
        {:square, _} ->
          IO.puts(String.duplicate("* ", total_size))
        {:triangle, _} ->
          IO.puts(String.duplicate("* ", value))
        {:diamond, _} when value <= size ->
          IO.puts(String.duplicate("* ", value))
        {:diamond, _} ->
          spaces = String.duplicate("  ", value - size)
          stars = String.duplicate("* ", total_size - value)
          IO.puts("#{spaces}#{stars}")
      end
    end)

    IO.puts("")
  end
end

WeeklyChallenge2022.main() ```