Home > Software engineering >  How to print a pyramid of full pyramids?
How to print a pyramid of full pyramids?

Time:10-31

I'm a newbie programmer, and we were recently tasked with printing a full pyramid of asterisks with the number of rows being based on the input, then printing a pyramid of those pyramids.

Basically the output I'm expecting when the user inputs Number of Rows: 4 is:

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

This is the best that I could work on using looping.

rowInput = int(input("Number of Rows: "))
rowCount = 0
min = rowInput
max = rowInput

while (rowCount < rowInput):
  digit = 1
  while (digit < min): 
    print(end=" ")
    digit  = 1
  while (digit >= min and digit <= max):
    print(end="*")
    digit  = 1
    
  print()
  min -= 1
  max  = 1
  rowCount  = 1

Input:

Number of Rows: 4

Output:

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

I think that it could work with another loop on top, but I can't think of a viable way to increment the spaces between two triangles. Are there any other options?

CodePudding user response:

You could build a list of lines to print using a nested list comprehension but it's a bit convoluted.

rows = 4

lines = [" "*(rows-r//2-1)*(2*rows-1)    f"{'*'*i:^{2*rows-1}}"*r
         for r in range(1,2*rows 1,2)
         for i in range(1,2*rows 1,2)]

print(*lines,sep='\n')

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

For easier to understand code, for-loops are probably better:

rows = 4

width = 2*rows-1                          # width of one pyramid
for r in range(1,rows 1):                 # row with r pyramids
    indent = " "*(rows-r)*width           # indent pyramids
    for p in range(rows):                 # pyramid lines 
        fill = "*"*(2*p 1)                # filled width/stars for line
        print(indent (2*r-1)*f"{fill:^{width}}") # repeated pyramid stars

Another good way to approach this is to decompose the problem into smaller parts that you implement in individual function. Combining the functions that perform small tasks makes it easier to implement and test your solution:

# produce lines for one pyramid
def pyramid(n):
    return [f"{'*'*r:^{2*n-1}}" for r in range(1,2*n 1,2)]

# combine pyramids horizontally
def pyramidRow(count,n): 
    return [count*line for line in pyramid(n)]

width = 2*rows-1                          # width of one pyramid
for r in range(1,rows 1):                 # row of r pyramids
    indent = " "*width*(rows-r)           # indent pyramids 
    for line in pyramidRow(2*r-1,rows):   # print repeated pyramids
        print(indent line)
  • Related