Page 321 - Computer Science V2.0 Class 11
P. 321
Sample Output:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Fig 11.10 :Sharp Diamond
11.7.4 Hollow Diamond
Now it is time to apply our learning to draw a hollow diamond pattern. Fig 11.11 shows a hollow diamond comprising
11 rows (six rows in a hollow upper isosceles triangle and five rows in an inverted hollow isosceles triangle). To build a
hollow upper isosceles triangle, nUpper is set to 6. In the first row, we need 5 (=6-1) spaces followed by one asterisk.
For the next row, we need 4 (=6-2) leading spaces. The leading spaces are followed by two asterisks separated by one
space, in all 3 (=2×2-1) characters. For the next row, we need 3(=6-3) leading spaces. The leading spaces are followed
th
by two asterisks separated by three spaces, in all and 5 (=2×3-1) characters. Thus, in i row we need nUpper-i
leading spaces. The leading spaces are followed by two asterisks, separated by an appropriate number of spaces. To
draw the inverted isosceles triangle, the number of leading spaces decreases by one in each subsequent row. We
incorporate these details into the function hollowDiamond(nRows, symbol).
Program 11.18 To display a hollow diamond.
01 def hollowDiamond(nRows, symbol):
02 '''
03 Objective: To display a hollow diamond
04 Inputs:
05 nRows : number of rows
06 symbol: symbol to be printed
07 Return value: None
08 '''
09 assert nRows%2==1, 'A diamond must have odd no of rows'
10 # Upper part of hollow diamond
11 nUpper = nRows//2 +1
12 for i in range(1, nUpper+1):
13 print(' ' * (nUpper-i), end = '')
14
15 for j in range(1, 2*i):
16 if j==1 or j==2*i-1:
17 print(symbol, end = '')
18 else:
19 print(' ', end = '')
20 print()
21 # Lower part of hollow diamond
22 nInverted = nRows//2
23 for i in range(nInverted,0,-1):
24 print(' ' * (nInverted-i+1), end = '')
25 for j in range(1, 2*i):
26 if j==1 or j==2*i-1:
27 print(symbol, end = '')
28 else:
Looping in Python 307

