Menu
Coddy logo textTech

Flat Asterisk Pyramid

Part of the Fundamentals section of Coddy's Swift journey — lesson 84 of 86.

challenge icon

Challenge

Easy
Write a function flatPyramid that takes rows and returns a string representing a flat asterisk pyramid.

The pyramid grows from 1 asterisk up to the specified number of rows, then shrinks back down to 1. Each row should be on its own line, separated by newline characters (\n).

Logic:

  1. Build the ascending part: rows with 1, 2, 3, ... up to rows asterisks
  2. Build the descending part: rows with rows - 1, rows - 2, ... down to 1 asterisk
  3. Join all rows with newline characters

Example: For rows = 3, the output should be:

*
**
***
**
*

This is returned as the string: "*\n**\n***\n**\n*"

Parameters:

  • rows (Int): The maximum width of the pyramid (the middle row)

Returns: A string containing the complete pyramid pattern with rows separated by \n

Try it yourself

func flatPyramid(rows: Int) -> String {
    // Write code here
}

All lessons in Fundamentals