Answer to Question #164507 in Python for hemanth

Question #164507

Given the number of rows N, write a program to print the hallow diamond pattern similar to the pattern shown below.

    A 
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A

The input will be a single line containing a positive integer (N).


1
Expert's answer
2021-02-17T14:55:46-0500
alphabetList=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
diamond =[]
#get the number of rows from the user
rows=-1
while rows<1 or rows>26:
    rows=int(input("Enter the number of rows N: "))
for i in range(0, rows):
    #add letters
    diamond.append("")
    for j in range(0, rows-i):
        diamond[i] += " "
    diamond[i] += alphabetList[i];
    if alphabetList[i] != 'A':
        #add spaces between letters
        for j in range(0, 2 * i - 1):
            diamond[i] += " "
        diamond[i] += alphabetList[i]
    #print the first part of the diamond        
    print(diamond[i])
#print the second part of the diamond
for i in reversed(range(0,rows-1)):
    print(diamond[i])

Example

Enter the number of rows N: 10
          A
         B B
        C   C
       D     D
      E       E
     F         F
    G           G
   H             H
  I               I
 J                 J
  I               I
   H             H
    G           G
     F         F
      E       E
       D     D
        C   C
         B B
          A

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS