Answer to Question #177978 in Python for sravani

Question #177978

Hollow Diamond

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).Output


The output should be (2*N - 1) rows and (2*N - 1) columns containing the alphabet characters in the hollow diamond pattern.Explanation


For example, if the given number is 5, the pattern should contain 9 rows and 9 columns as shown below.

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

note:left side spaces shouldnt be..


1
Expert's answer
2021-04-03T09:51:32-0400
alphabets = ['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 array
diamond = []

rows = -1
# Prompt user to enter number of rows
while rows < 1 or rows > 26:
    rows = int(input("Enter number of rows N: "))
for i in range(0, rows):
    # Add letters to the diamond
    diamond.append("")
    for j in range(0, rows - i):
        diamond[i] += " "
    diamond[i] += alphabets[i];
    if alphabets[i] != 'A':
        # Put spaces between letters
        for j in range(0, 2 * i - 1):
            diamond[i] += " "
        diamond[i] += alphabets[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])

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