Answer to Question #164831 in Python for hemanth

Question #164831

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.


1
Expert's answer
2021-02-21T20:01:46-0500

# array of letters

L=['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']


# input value from a user

n=int(input('Input N>'))

print('N='+str(n))


# print lines from 1 to N

for i in range(n):

   s=''

   for j in range(n-1-i):

       s=s+' '

   s=s+L[i%26]

   for j in range(i-1):

       s=s+' '

   if i>0:

       s=s+' '+L[i%26]

   print(s)


# print lines from N+1 to 2*N-1

for i in reversed(range(n-1)):

   s=''

   for j in range(n-1-i):

       s=s+' '

   s=s+L[i%26]

   for j in range(i-1):

       s=s+' '

   if i>0:

       s=s+' '+L[i%26]

   print(s)


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