Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add butterfly pattern implementation #12151 #12493

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions graphics/butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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):
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):
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))
8 changes: 8 additions & 0 deletions graphics/test_butterfly_pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from graphics.butterfly_pattern import butterfly_pattern


def test_butterfly_pattern():
expected_output = (
"* *\n" "** **\n" "*** ***\n" "** **\n" "* *"
)
assert butterfly_pattern(3) == expected_output
Loading