Skip to content

Commit

Permalink
Add finalized butterfly pattern implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
aydinomer00 committed Jan 2, 2025
1 parent 459613e commit f99e68e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
29 changes: 20 additions & 9 deletions graphics/butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
def butterfly_pattern(n):
def butterfly_pattern(n: int) -> str:
"""
Creates a butterfly pattern of size n and returns it as a string.
"""
result = []

# Upper part
for i in range(1, n + 1):
print("*" * i, end="")
print(" " * (2 * (n - i)), end="")
print("*" * i)
left_stars = "*" * i
spaces = " " * (2 * (n - i + 2))
right_stars = "*" * i
result.append(left_stars + spaces + right_stars)

# Lower part
for i in range(n - 1, 0, -1):
print("*" * i, end="")
print(" " * (2 * (n - i)), end="")
print("*" * i)
left_stars = "*" * i
spaces = " " * (2 * (n - i + 2))
right_stars = "*" * i
result.append(left_stars + spaces + right_stars)

return "\n".join(result)


if __name__ == "__main__":
n = int(input("Enter the size of the butterfly pattern: "))
print(butterfly_pattern(n))

n = int(input("Enter the size: "))
butterfly_pattern(n)
11 changes: 11 additions & 0 deletions graphics/test_butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from graphics.butterfly_pattern import butterfly_pattern

def test_butterfly_pattern():

Check failure on line 3 in graphics/test_butterfly_pattern.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

graphics/test_butterfly_pattern.py:1:1: I001 Import block is un-sorted or un-formatted
expected_output = (
"* *\n"
"** **\n"
"*** ***\n"
"** **\n"
"* *"
)
assert butterfly_pattern(3) == expected_output

0 comments on commit f99e68e

Please sign in to comment.