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

Program 11.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 11.9: Diamond

              11.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 11.9. In Function 11.10, we dropped one row
              from lower inverted isosceles triangle by invoking invertedIsoTriangle(nRows//2-1, symbol).

               Program 11.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,'*')





               306   Touchpad Computer Science (Ver. 2.0)-XI
   315   316   317   318   319   320   321   322   323   324   325