Minuet
Minuet

Reputation: 93

Creating pyramids within a pyramid

I have a programming exercise where we have to create a program that produces a pyramid within a pyramid figure depending on the given height (user input) of one of the mini triangle.

I've managed to create the top layer pyramid but I'm having a hard time when it comes to the succeeding layers, the pyramids become out of place.

This is what I've been working on so far:

h = int(input())
for r in range (1, h+1):
    for i in range(1,h+1):
        if r == 1:
            print (((" " * ((((h * 2) - 1) * h) - h - i + 1))  + ("*" * (i * 2 - 1)))*(r*2-1))
        else:
            print (((" " * ((((h * 2) - 1) * h) - h - i + 1 - (r*2-1)))  + ("*" * (i * 2 - 1)))*(r*2-1))

This should be the expected output when 'h' is 3

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

But I'm getting this instead:

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

Upvotes: 2

Views: 419

Answers (2)

Nouman
Nouman

Reputation: 7303

Rather than using complex methods, I made another way which uses strings and lists.

def pyramid(h):
    rows = ["  *  ", " *** ", "*****"]
    for i in range(h):
        out = []
        for x in range(1,len(rows)+1):
            temp = ""
            for _ in range(i+1):temp += rows[x-1]
            for _ in range(i, 0, -1):temp += rows[x-1]
            out.append(temp+"\n")
        print("".join([("     "*(h-1-i))+val for val in out]), end = "")

When testing with h = 3 as:

pyramid(3)

The output is, as expected:

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

Upvotes: 0

mickey
mickey

Reputation: 2188

Here's a function you can use. Defining the individual components that make up the pyramid helped to figure out the right formulas to use.

def pyramid(h = 1): 
    pyr = ['']*h
    base = 2*h - 1
    for i in range(h):
        pyr[i] = " " * (h-i-1) + "*"*(2*i+1) + " " * (h-i-1)
    for i in range(h):
        for j in range(h):
            print(" " * base * (h-i-1) + pyr[j]*(2*i+1) + " " * base * (h-i-1))

Example with h = 4

>>> pyramid(4)
                        *                        
                       ***                       
                      *****                      
                     *******                     
                 *      *      *                 
                ***    ***    ***                
               *****  *****  *****               
              *********************              
          *      *      *      *      *          
         ***    ***    ***    ***    ***         
        *****  *****  *****  *****  *****        
       ***********************************       
   *      *      *      *      *      *      *   
  ***    ***    ***    ***    ***    ***    ***  
 *****  *****  *****  *****  *****  *****  ***** 
*************************************************

Upvotes: 2

Related Questions