Page 317 - Computer Science V2.0 Class 11
P. 317

11.7 Printing Patterns

                 In this section, we will learn to display some nice patterns. We begin with a right triangle of asterisks.
                 Right Triangle

                 A right triangle has one symbol in the first row, two in the second row, three in the third row, and so on. A function to
                 print such a triangle will only require two parameters, the number of rows (say, nRows) and the symbol (say, symbol)
                 to be used in the pattern (see program 11.11).

                 Program 11.11 Write a function rtTriangle(nRows, symbol) to display the right triangular pattern of a symbol.
                   01 def rtTriangle(nRows, symbol):
                   02     '''
                   03     Objective: To display the right triangular pattern of a symbol
                   04     Inputs:
                   05       nRows : number of rows
                   06       symbol: symbol to be printed
                   07     Return value: None
                   08     '''
                   09     for i in range(1, nRows + 1):
                   10         for j in range(1, i + 1):
                   11             print(symbol, end = '')
                   12         print()
                   13 rtTriangle(5, '*')
                 Sample Output:

                      *
                      **
                      ***
                      ****
                      *****
                 Even though program 11.11 shows how to use nested for-statements, there was a simpler way to do it using the
                 repetition operator *. Recall that the expression symbol*i yields a string of length i. So, using the repetition operator,
                 program 11.11 may be rewritten as program 11.12.

                 Program 11.12 To display the right triangular pattern of a symbol using string.

                   01 def rtTriangle(nRows, symbol):
                   02     '''
                   03     Objective: To display the right triangular pattern of a symbol
                   04     Inputs:
                   05       nRows : number of rows
                   06       symbol: symbol to be printed
                   07     Return value: None
                   08     '''
                   09     for i in range(1, nRows + 1):
                   10         print(symbol*i)
                   11         print()
                 Inverted isosceles Triangle

                 Next, let us write a function to print an inverted isosceles triangle using a given symbol (say, symbol). Fig 11.6 shows
                 an inverted isosceles triangle comprising six rows using asterisks. Note that the first row does not have any leading
                 spaces, the 2nd row has one leading space, 3rd row has two leading spaces, and so on. Finally, the last row (6th
                 row) has 5 (=6-1) spaces. Thus, beginning with zero leading spaces in the first row, the number of leading spaces
                 (say, nSpaces) increases by one in each following row. Furthermore, the first row has 11 (=6×2–1) asterisks, the
                 second row has 9 asterisks, and so on. Finally, the sixth row has one asterisk. Thus, beginning with 2×nRows-1 asterisks
                 in the first row, the number of asterisks (say, nSymbols) decreases by two in each following row. We incorporate
                 these details into the function invertedIsoTriangle(nRows, symbol).

                                                                                                   Looping in Python  303
   312   313   314   315   316   317   318   319   320   321   322