Skip to content

Commit 876087b

Browse files
SaiHarshaKHarsha Kottapalli
and
Harsha Kottapalli
authored
Add DocTests to magicdiamondpattern.py (TheAlgorithms#10135)
* magicdiamondpattern doctest * remove start part --------- Co-authored-by: Harsha Kottapalli <[email protected]>
1 parent 12e8e9c commit 876087b

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

other/magicdiamondpattern.py

+50-26
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,76 @@
44
# Function to print upper half of diamond (pyramid)
55
def floyd(n):
66
"""
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'
918
"""
19+
result = ""
1020
for i in range(n):
1121
for _ in range(n - i - 1): # printing spaces
12-
print(" ", end="")
22+
result += " "
1323
for _ in range(i + 1): # printing stars
14-
print("* ", end="")
15-
print()
24+
result += "* "
25+
result += "\n"
26+
return result
1627

1728

1829
# Function to print lower half of diamond (pyramid)
1930
def reverse_floyd(n):
2031
"""
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 '
2343
"""
44+
result = ""
2445
for i in range(n, 0, -1):
2546
for _ in range(i, 0, -1): # printing stars
26-
print("* ", end="")
27-
print()
47+
result += "* "
48+
result += "\n"
2849
for _ in range(n - i + 1, 0, -1): # printing spaces
29-
print(" ", end="")
50+
result += " "
51+
return result
3052

3153

3254
# Function to print complete diamond pattern of "*"
3355
def pretty_print(n):
3456
"""
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 '
3768
"""
3869
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
4374

4475

4576
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

Comments
 (0)