Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0040ad4

Browse files
aydinomer00pre-commit-ci[bot]MaximSmolskiy
authored
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>
1 parent f04d308 commit 0040ad4

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎graphics/butterfly_pattern.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def butterfly_pattern(n: int) -> str:
2+
"""
3+
Creates a butterfly pattern of size n and returns it as a string.
4+
5+
>>> print(butterfly_pattern(3))
6+
* *
7+
** **
8+
*****
9+
** **
10+
* *
11+
>>> print(butterfly_pattern(5))
12+
* *
13+
** **
14+
*** ***
15+
**** ****
16+
*********
17+
**** ****
18+
*** ***
19+
** **
20+
* *
21+
"""
22+
result = []
23+
24+
# Upper part
25+
for i in range(1, n):
26+
left_stars = "*" * i
27+
spaces = " " * (2 * (n - i) - 1)
28+
right_stars = "*" * i
29+
result.append(left_stars + spaces + right_stars)
30+
31+
# Middle part
32+
result.append("*" * (2 * n - 1))
33+
34+
# Lower part
35+
for i in range(n - 1, 0, -1):
36+
left_stars = "*" * i
37+
spaces = " " * (2 * (n - i) - 1)
38+
right_stars = "*" * i
39+
result.append(left_stars + spaces + right_stars)
40+
41+
return "\n".join(result)
42+
43+
44+
if __name__ == "__main__":
45+
n = int(input("Enter the size of the butterfly pattern: "))
46+
print(butterfly_pattern(n))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /