Skip to content

Commit f99e68e

Browse files
committed
Add finalized butterfly pattern implementation and test
1 parent 459613e commit f99e68e

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

graphics/butterfly_pattern.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
def butterfly_pattern(n):
1+
def butterfly_pattern(n: int) -> str:
2+
"""
3+
Creates a butterfly pattern of size n and returns it as a string.
4+
"""
5+
result = []
6+
27
# Upper part
38
for i in range(1, n + 1):
4-
print("*" * i, end="")
5-
print(" " * (2 * (n - i)), end="")
6-
print("*" * i)
9+
left_stars = "*" * i
10+
spaces = " " * (2 * (n - i + 2))
11+
right_stars = "*" * i
12+
result.append(left_stars + spaces + right_stars)
713

814
# Lower part
915
for i in range(n - 1, 0, -1):
10-
print("*" * i, end="")
11-
print(" " * (2 * (n - i)), end="")
12-
print("*" * i)
16+
left_stars = "*" * i
17+
spaces = " " * (2 * (n - i + 2))
18+
right_stars = "*" * i
19+
result.append(left_stars + spaces + right_stars)
20+
21+
return "\n".join(result)
22+
1323

24+
if __name__ == "__main__":
25+
n = int(input("Enter the size of the butterfly pattern: "))
26+
print(butterfly_pattern(n))
1427

15-
n = int(input("Enter the size: "))
16-
butterfly_pattern(n)

graphics/test_butterfly_pattern.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from graphics.butterfly_pattern import butterfly_pattern
2+
3+
def test_butterfly_pattern():
4+
expected_output = (
5+
"* *\n"
6+
"** **\n"
7+
"*** ***\n"
8+
"** **\n"
9+
"* *"
10+
)
11+
assert butterfly_pattern(3) == expected_output

0 commit comments

Comments
 (0)