Elixir Today: Create a Reverse Pyramid Pattern using Elixir

Photo by Denys Nevozhai on Unsplash

Elixir Today: Create a Reverse Pyramid Pattern using Elixir

Pau Riosa's photo
Pau Riosa
·May 10, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Process

  • create a file named reverse_pyramid.ex
  • write the code
reverse_pyramid = fn number ->
  for i <- 0..number do
    # for printing spaces

    spaces =
      for _j <- 0..i do
        " "
      end

    # for printing star
    star_number = 2 * (number - i) - 1

    star =
      for k when k < star_number <- 0..star_number do
        "*"
      end

    spaces ++ star
  end
  |> Enum.into("", fn string ->
    string = Enum.join(string)
    "#{string}\n"
  end)
end

IO.puts(reverse_pyramid.(10))
IO.puts(reverse_pyramid.(5))
  • run elixir reverse_pyramid.ex

Result

 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *


 *********
  *******
   *****
    ***
     *

Happy Coding!

 
Share this