Page 259 - Computer Science Class 11 With Functions
P. 259

Program 10.16 Write a function diamond(nRows,symbol) to display the diamond pattern of a symbol.

              01 def diamond(nRows, symbol):
              02     '''
              03     Objective: To display a diamond pattern
              04     Inputs:
              05       nRows : number of rows
              06       symbol: symbol to be used in the pattern
              07     Return value: None
              08     '''
              09     #Approach: Ensure nRows is even
              10     #Invoke functions isoscelesTriangle, invertedIsoTriangle
              11     assert nRows%2==0, 'A diamond must have even no of rows'
              12     isoscelesTriangle(nRows//2, symbol)
              13     invertedIsoTriangle(nRows//2, symbol)
             >>> diamond(10, '8')
            Sample Output:
                      8
                     888
                    88888
                   8888888
                  888888888
                 88888888888
                 88888888888
                  888888888
                   8888888
                    88888
                     888
                      8
            Fig 10.9: Diamond

            10.7.3 Sharp Diamond

            Note that the diamond is not sharp at the horizontal edges. If you are happy with this form of diamond, nothing needs
            to be done. If not, we must sharpen the edges. To sharpen the edges, we simply need to drop either of the two rows
            with the maximum number of symbols, resulting from execution of isoscelesTriangle(nRows//2, symbol)
            and invertedIsoTriangle(nRows//2, symbol) in Function 10.9. In Function 10.10, we dropped one row
            from lower inverted isosceles triangle by invoking invertedIsoTriangle(nRows//2-1, symbol).

            Program 10.17 Write a function sharpDiamond(nRows,symbol) to display the sharp diamond pattern of a symbol.

              01 def sharpDiamond(nRows, symbol):
              02     '''
              03     Objective: To display a diamond pattern
              04     Inputs:
              05       nRows : number of rows
              06       symbol: symbol to be used in the pattern
              07     Return value: None
              08     '''
              09     #Approach: Ensure nRows is even
              10     #Invoke functions isocelesTriangle, invertedIsoTriangle
              11     assert nRows%2==0, 'A diamond must have even no of rows'
              12     isoscelesTriangle(nRows//2, symbol)
              13     invertedIsoTriangle(nRows//2-1, symbol)
             >>> sharpDiamond(12,'*')





                                                                                              Looping in Python  257
   254   255   256   257   258   259   260   261   262   263   264