Add butterfly pattern implementation (#12493)

* Add butterfly pattern implementation

* Add butterfly pattern implementation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add finalized butterfly pattern implementation and test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Delete graphics/test_butterfly_pattern.py

* Update butterfly_pattern.py

* Update butterfly_pattern.py

* Update butterfly_pattern.py

* Update butterfly_pattern.py

* Update butterfly_pattern.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
aydinomer00 2025-01-15 00:24:36 +03:00 committed by GitHub
parent f04d308431
commit 0040ad47f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,46 @@
def butterfly_pattern(n: int) -> str:
"""
Creates a butterfly pattern of size n and returns it as a string.
>>> print(butterfly_pattern(3))
* *
** **
*****
** **
* *
>>> print(butterfly_pattern(5))
* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
"""
result = []
# Upper part
for i in range(1, n):
left_stars = "*" * i
spaces = " " * (2 * (n - i) - 1)
right_stars = "*" * i
result.append(left_stars + spaces + right_stars)
# Middle part
result.append("*" * (2 * n - 1))
# Lower part
for i in range(n - 1, 0, -1):
left_stars = "*" * i
spaces = " " * (2 * (n - i) - 1)
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))