Answer to Question #167918 in Python for srikanth

Question #167918
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

Sample Input 1
5
Sample Output 1
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A

Sample Input 2
3
Sample Output 2
  A
 B B
C   C
 B B
  A
1
Expert's answer
2021-03-01T21:35:24-0500
# Diamond.py

N = int(input())

left = N-1
mid = -1
ch = 'A'
print(' '*left, ch, sep='')

for _ in range(N-1):
    left -= 1
    mid += 2
    ch = chr(ord(ch)+1)
    print(' '*left, ch, ' '*mid, ch, sep='')
    
for _ in range(N-2):
    left += 1
    mid -= 2
    ch = chr(ord(ch)-1)
    print(' '*left, ch, ' '*mid, ch, sep='')

left += 1
ch = 'A'
print(' '*left, ch, sep='')

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

srikanth
02.03.21, 05:59

thank you

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS