Print the upper half of a diamond pattern with '*' characters. Args: n (int): Size of the pattern. Examples: >>> floyd(3) ' * \\n * * \\n* * * \\n' >>> floyd(5) ' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n'
(n)
| 3 | |
| 4 | # Function to print upper half of diamond (pyramid) |
| 5 | def floyd(n): |
| 6 | """ |
| 7 | Print the upper half of a diamond pattern with '*' characters. |
| 8 | |
| 9 | Args: |
| 10 | n (int): Size of the pattern. |
| 11 | |
| 12 | Examples: |
| 13 | >>> floyd(3) |
| 14 | ' * \\n * * \\n* * * \\n' |
| 15 | |
| 16 | >>> floyd(5) |
| 17 | ' * \\n * * \\n * * * \\n * * * * \\n* * * * * \\n' |
| 18 | """ |
| 19 | result = "" |
| 20 | for i in range(n): |
| 21 | for _ in range(n - i - 1): # printing spaces |
| 22 | result += " " |
| 23 | for _ in range(i + 1): # printing stars |
| 24 | result += "* " |
| 25 | result += "\n" |
| 26 | return result |
| 27 | |
| 28 | |
| 29 | # Function to print lower half of diamond (pyramid) |