Answer to Question #176599 in Python for adhi chinna

Question #176599

AD


ASSIGNMENT - 2


DESCRIPTION


HINTS & SOLUTION


SUBMISSIONS


DISCUSS


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
1
Expert's answer
2021-04-02T13:40:16-0400
n = int(input())
leftSpaces = n-1
middleSpaces = -1


#dislay the first letter
print(leftSpaces*" ", "A", sep="")
letter = "A"
n=n-1
#dislay top part of the diamond 
for i in range(n):
    # convert char into int
    intLetter=ord(letter)+1
    # convert int into char and get the next letter
    letter = chr(intLetter)
    leftSpaces = leftSpaces-1
    middleSpaces = middleSpaces+2
    #dislay the letters
    print(leftSpaces*" ", letter, middleSpaces*" ", letter, sep="")
#dislay bottom part of the diamond
n=n-1
for i in range(n):
    # convert char into int
    intLetter=ord(letter)-1
    # convert int into char and get the next letter
    letter = chr(intLetter)
    leftSpaces =leftSpaces + 1
    middleSpaces =middleSpaces - 2
    #dislay the letters
    print(leftSpaces*" ", letter, middleSpaces*" ", letter, sep="")
#dislay the last letter
print((leftSpaces+1)*" ", "A", 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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS