|
4 | 4 | # Function to print upper half of diamond (pyramid)
|
5 | 5 | def floyd(n):
|
6 | 6 | """
|
7 |
| - Parameters: |
8 |
| - n : size of pattern |
| 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' |
9 | 18 | """
|
| 19 | + result = "" |
10 | 20 | for i in range(n):
|
11 | 21 | for _ in range(n - i - 1): # printing spaces
|
12 |
| - print(" ", end="") |
| 22 | + result += " " |
13 | 23 | for _ in range(i + 1): # printing stars
|
14 |
| - print("* ", end="") |
15 |
| - print() |
| 24 | + result += "* " |
| 25 | + result += "\n" |
| 26 | + return result |
16 | 27 |
|
17 | 28 |
|
18 | 29 | # Function to print lower half of diamond (pyramid)
|
19 | 30 | def reverse_floyd(n):
|
20 | 31 | """
|
21 |
| - Parameters: |
22 |
| - n : size of pattern |
| 32 | + Print the lower half of a diamond pattern with '*' characters. |
| 33 | +
|
| 34 | + Args: |
| 35 | + n (int): Size of the pattern. |
| 36 | +
|
| 37 | + Examples: |
| 38 | + >>> reverse_floyd(3) |
| 39 | + '* * * \\n * * \\n * \\n ' |
| 40 | +
|
| 41 | + >>> reverse_floyd(5) |
| 42 | + '* * * * * \\n * * * * \\n * * * \\n * * \\n * \\n ' |
23 | 43 | """
|
| 44 | + result = "" |
24 | 45 | for i in range(n, 0, -1):
|
25 | 46 | for _ in range(i, 0, -1): # printing stars
|
26 |
| - print("* ", end="") |
27 |
| - print() |
| 47 | + result += "* " |
| 48 | + result += "\n" |
28 | 49 | for _ in range(n - i + 1, 0, -1): # printing spaces
|
29 |
| - print(" ", end="") |
| 50 | + result += " " |
| 51 | + return result |
30 | 52 |
|
31 | 53 |
|
32 | 54 | # Function to print complete diamond pattern of "*"
|
33 | 55 | def pretty_print(n):
|
34 | 56 | """
|
35 |
| - Parameters: |
36 |
| - n : size of pattern |
| 57 | + Print a complete diamond pattern with '*' characters. |
| 58 | +
|
| 59 | + Args: |
| 60 | + n (int): Size of the pattern. |
| 61 | +
|
| 62 | + Examples: |
| 63 | + >>> pretty_print(0) |
| 64 | + ' ... .... nothing printing :(' |
| 65 | +
|
| 66 | + >>> pretty_print(3) |
| 67 | + ' * \\n * * \\n* * * \\n* * * \\n * * \\n * \\n ' |
37 | 68 | """
|
38 | 69 | if n <= 0:
|
39 |
| - print(" ... .... nothing printing :(") |
40 |
| - return |
41 |
| - floyd(n) # upper half |
42 |
| - reverse_floyd(n) # lower half |
| 70 | + return " ... .... nothing printing :(" |
| 71 | + upper_half = floyd(n) # upper half |
| 72 | + lower_half = reverse_floyd(n) # lower half |
| 73 | + return upper_half + lower_half |
43 | 74 |
|
44 | 75 |
|
45 | 76 | if __name__ == "__main__":
|
46 |
| - print(r"| /\ | |- | |- |--| |\ /| |-") |
47 |
| - print(r"|/ \| |- |_ |_ |__| | \/ | |_") |
48 |
| - K = 1 |
49 |
| - while K: |
50 |
| - user_number = int(input("enter the number and , and see the magic : ")) |
51 |
| - print() |
52 |
| - pretty_print(user_number) |
53 |
| - K = int(input("press 0 to exit... and 1 to continue...")) |
54 |
| - |
55 |
| - print("Good Bye...") |
| 77 | + import doctest |
| 78 | + |
| 79 | + doctest.testmod() |
0 commit comments