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

Program 11.13 To display the inverted isosceles triangular pattern of a symbol.

                01 def invertedIsoTriangle(nRows, symbol):
                02     '''
                03     Objective: To display an inverted isosceles triangular pattern
                04     Inputs:
                05       nRows : number of rows
                06       symbol: symbol to be printed
                07     Return value: None
                08     '''
                09     #Approach: In each line,
                10     #          first print enough blanks, then enough symbols
                11
                12     nSpaces = 0
                13     nSymbols = 2 * nRows - 1
                14
                15     for i in range(nRows):
                16         print(' ' * nSpaces, end = '')
                17         print(symbol * nSymbols)
                18         nSpaces = nSpaces + 1
                19          nSymbols = nSymbols - 2
                20 invertedIsoTriangle(6, '*')

              Sample Output:
                    ***********
                     *********
                      *******
                       *****
                        ***
                         *
              Fig 11.6: Inverted Isosceles Triangle
              Next, let us write a function to print an isosceles triangle using a given symbol (say, symbol). Fig 11.7 shows an
              isosceles triangle comprising six rows using asterisks. Note that the last row (6  row) does not have any leading
                                                                                       th
              spaces, 5  row has one leading space, the 4  row has two leading spaces, and so on. Finally, the first row has 5 (=6-1)
                      th
                                                     th
              spaces. Thus, i  row has nRows-i leading spaces. Furthermore, the first row has one asterisk, the second row has
                            th
                                                                                   th
              3 asterisks, and so on. Finally, the sixth row has 11 (=6×2-1) asterisks. Thus, i  row has 2×i-1 asterisks. Now we are
              ready to write a function to display an isosceles triangle.
               Program 11.14 Write a function isoscelesTriangle(nRows,symbol) to display the isosceles triangular pattern
               of a symbol.

                01 def isoscelesTriangle(nRows, symbol):
                02     '''
                03     Objective: To display an isosceles triangular pattern of a symbol
                04     Inputs:
                05       nRows : number of rows
                06       symbol: symbol to be printed
                07     Return value: None
                08     '''
                09     #Approach: In each line,
                10     #          first print enough blanks, then enough symbols
                11
                12     nSpaces = nRows-1
                13     nSymbols = 1
                14     for row in range(1, nRows + 1):
                15         print(' ' * nSpaces, end = '')
                16         print(symbol * nSymbols)


               304   Touchpad Computer Science (Ver. 2.0)-XI
   313   314   315   316   317   318   319   320   321   322   323