Python Program to print the sample ‘G’

[ad_1]

View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

On this article, we’ll discover ways to print the sample G utilizing stars and white-spaces. Given a quantity n, we’ll write a program to print the sample G over n strains or rows.
Examples: 
 

Enter : 7
Output :
  ***  
 *     
 *     
 * *** 
 *   * 
 *   * 
  ***  

Enter : 9
Output :
  *****  
 *       
 *       
 *       
 *   *** 
 *     * 
 *     * 
 *     * 
  *****  

 

On this program, we’ve got used the easy logic of iteration over strains to create the sample G. Please have a look at the picture beneath which represents the sample G within the type of a 2-d matrix, the place mat[i][j] = ‘ij’: 
 

G

If we attempt to analyze this image with a (row, column) matrix and the circles symbolize the place of stars within the sample G, we’ll be taught the steps. Right here we’re performing the operations column-wise. So for the primary line of stars, we set the primary if situation, the place the row place with 0 and (n-1) received’t get the celebrities and all different rows from 1 to (n-1), will get the celebrities. Equally, for the second, third and fourth column we would like stars on the place row = 0 and row = (n-1). The opposite steps are self-explanatory and will be understood from the place of rows and columns within the diagram.
Beneath is the implementation of above thought: 
 

Python

def Sample(line):

    pat=""

    for i in vary(0,line):    

        for j in vary(0,line):     

            if ((j == 1 and i != 0 and i != line-1) or ((i == 0 or

                i == line-1) and j > 1 and j < line-2) or (i == ((line-1)/2)

                and j > line-5 and j < line-1) or (j == line-2 and

                i != 0 and i != line-1 and i >=((line-1)/2))):  

                pat=pat+"*"   

            else:      

                pat=pat+" "   

        pat=pat+"

"   

    return pat

   

line = 7

print(Sample(line))

Output: 
 

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

Time Complexity: O(n2), the place n represents the given enter.
Auxiliary House: O(1), no additional house is required, so it’s a fixed.

Please refer full article on Program to print the sample ‘G’ for extra particulars!

[ad_2]

Leave a Reply