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
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="")
Comments
Leave a comment